Divide Collection List into sub groups

Not sure when this might be useful for people - but I have a need right now and found the answer so I wanted to share and also to keep it on file for myself!

$.fn.chunk = function(size) {
  var arr = [];
  for (var i = 0; i < this.length; i += size) {
    arr.push(this.slice(i, i + size));
  }
  return this.pushStack(arr, "chunk", size);
}

$(".gallery-list-list .gallery-list-item").chunk(3).wrap('<div class="gallery-list-block"></div>');

Here’s a jsfiddle of it in action - http://jsfiddle.net/diarmuids/968j0rth/6/

1 Like

You sir, made my day! This was exactly what I was looking for. Trying to distibute list items in a grid, with x-items per grid cell. Thank you for sharing!