Created
October 9, 2014 12:01
-
-
Save adriancbo/b50bfce3d9e8e3ea57c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var addNth = (function () { | |
var len, i = 0, className, prevIndexes = []; | |
function isNew (el) { | |
return el.hasClass(className); // removed unnecessary parenthesis | |
} | |
return function (selector, html, nth, className ) { | |
var els = $( selector ); | |
className = className || 'test'; | |
if ( $.inArray(nth, prevIndexes) === -1 ) { | |
prevIndexes.push(nth); | |
$.each(els, function( index, el ) { | |
el = $(el); | |
if ( (i % nth) === 0 && i !== 0 ) { | |
if ( ! isNew(el) ) { | |
el.before( html ); | |
} | |
} | |
i++; | |
}); | |
i = 0; | |
} | |
} | |
})(); | |
addNth('div','<p class="test">Test</p>',3); | |
addNth('div','<p class="test">Test</p>',3); // won't add content |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment