Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save adriancbo/b50bfce3d9e8e3ea57c0 to your computer and use it in GitHub Desktop.
Save adriancbo/b50bfce3d9e8e3ea57c0 to your computer and use it in GitHub Desktop.
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