I have a list of elements (divs) preseded by a H3 tag
<h3></h3>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<h3></h3>
<div class="item"></div>
<div class="item"></div>
etc...
Using jQuery, I'd like to group every 3 divs (or less) followed by each h3 like this:
<h3></h3>
<div class=row>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
<div class=row>
<div class="item"></div>
</div>
<h3></h3>
<div class=row>
<div class="item"></div>
<div class="item"></div>
</div>
I tried a solution proposed here: Insert <div> for every 5 elements using Javascript but it obviously grouped ALL the divs. I also tried using ~ selector without any success:
var a = $('h3 ~ div.item');
for( var i = 0; i < a.length; i+=3 ) {
a.slice(i, i+3).wrapAll('<div class="row"></div>');
}
Any help will be highly appreciated.