Skip to content

Instantly share code, notes, and snippets.

@nielk
Created December 9, 2013 20:24
Show Gist options
  • Save nielk/7880158 to your computer and use it in GitHub Desktop.
Save nielk/7880158 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
</body>
</html>
var nthAdd = function(){
return function(html, selector, classname, nth) {
$(selector).each(function(i, el) {
if((i + 1) % 3 === 0 && !$(el).next().is(classname)) {
$(el).after(html);
}
});
};
}();
nthAdd('<div class="test">added item</div>','div:not(.test)','.test');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment