Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamturtle/9554a02282011304bfc1 to your computer and use it in GitHub Desktop.
Save adamturtle/9554a02282011304bfc1 to your computer and use it in GitHub Desktop.
/**
* Takes a jQuery collection and wraps each 'groupSize' items with 'wrapHtml'
* Example: $('#Example span').wrapInGroups('<div/>', 4);
* Will wrap every 4 span's inside #Example with a div
* See for yourself: http://jsfiddle.net/M9ZFh/
* Author: iMoses (imoses.g@gmail.com)
*/
(function($) {
$.fn.wrapInGroups = function(wrapHtml, groupSize) {
var selector = this.selector;
groupSize = groupSize || 1;
this.filter(':nth-child(' + groupSize + 'n+1)').each(function() {
$(this).nextAll(selector + ':lt(' + (groupSize - 1) + ')').andSelf().wrapAll(wrapHtml);
});
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment