Select subsets of elements, and wrap them in a div
- by scullytr
Hello, I have a set of selected elements in jQuery:
<div id="element1" class="element"></div>
<div id="element2" class="element"></div>
<div id="element3" class="element"></div>
<div id="element4" class="element"></div>
<div id="element5" class="element"></div>
<div id="element6" class="element"></div>
<div id="element7" class="element"></div>
<div id="element8" class="element"></div>
<div id="element9" class="element"></div>
<div id="element10" class="element"></div>
<div id="element11" class="element"></div>
<div id="element12" class="element"></div>
I would like to wrap these elements into groups of five like so:
<div class="wrapper">
<div id="element1" class="element"></div>
<div id="element2" class="element"></div>
<div id="element3" class="element"></div>
<div id="element4" class="element"></div>
<div id="element5" class="element"></div>
</div>
<div class="wrapper">
<div id="element6" class="element"></div>
<div id="element7" class="element"></div>
<div id="element8" class="element"></div>
<div id="element9" class="element"></div>
<div id="element10" class="element"></div>
</div>
<div class="wrapper">
<div id="element11" class="element"></div>
<div id="element12" class="element"></div>
</div>
Is there an efficient/easy way to do this? I imagine it would look something like this:
$('.element').wrapAll('<div class="wrapper"></div>');
But with a selector that selects the elements in groups of five.
Thanks!