Skip to content

Instantly share code, notes, and snippets.

@nickberens360
Last active August 17, 2017 18:03
Show Gist options
  • Save nickberens360/8213912 to your computer and use it in GitHub Desktop.
Save nickberens360/8213912 to your computer and use it in GitHub Desktop.
jquery: Wrap each n elements in jQuery found here: http://forrst.com/posts/Wrap_each_n_elements_in_jQuery-7zl
(function($){
$.fn.wrapChildren = function(options) {
var options = $.extend({
childElem : undefined,
sets : 1,
wrapper : 'div class="random"'
}, options || {});
if (options.childElem === undefined) return this;
return this.each(function() {
var elems = $(this).children(options.childElem);
var arr = [];
elems.each(function(i,value) {
arr.push(value);
if (((i + 1) % options.sets === 0) || (i === elems.length -1))
{
var set = $(arr);
arr = [];
set.wrapAll($("<" + options.wrapper + ">"));
}
});
});
}
})(jQuery);
$(function() {
$('.article-roll').wrapChildren({ childElem : 'li' , sets: 1, wrapper : 'div class="random"'});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment