jQuery Grouping Elements -
say have 10 elements , want create groups of 5, how can wrap x elements in jquery? here markup:
<div class="mainwrap"> <!-- wrap div --> <div class="item">item</div> <div class="item">item</div> <div class="item">item</div> <div class="item">item</div> <div class="item">item</div> <!-- close wrap --><!-- open new wrap --> <div class="item">item</div> <div class="item">item</div> <div class="item">item</div> <!-- close wrap --> </div><!-- mainwrap -->
dynamically? maybe like.
var grouping = []; var $newwrapping = $('<div class="mainwrap"></div>"); $('.mainwrap .item').each(function(index, value){ grouping.push($(value)); if (grouping.length > 4) { $newwrapping.append(grouping); grouping = []; } }); if (grouping.length > 0) { $newwrapping.append(grouping); } $('.mainwrap').replacewith($newwrapping);
Comments
Post a Comment