Skip to content

Instantly share code, notes, and snippets.

@seutje
Last active September 2, 2015 07:52
Show Gist options
  • Save seutje/8565f609d8b73354c2a3 to your computer and use it in GitHub Desktop.
Save seutje/8565f609d8b73354c2a3 to your computer and use it in GitHub Desktop.
Little jQuery plugin to wrap every X items.
/**
* Wraps every num elements with el.
*
* Usage: $('.foo').wrapEach('<div></div>', 5);
* Wraps every 5 .foo's with a <div>, defaults to 2.
*/
$.fn.wrapEach = function(el, num) {
num = num || 2;
for (var i = 0; i < this.length; i += num) {
this.slice(i, i + num).wrapAll(el);
}
return this;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment