Last active
August 17, 2017 18:03
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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