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

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -