Select subsets of elements, and wrap them in a div

Posted by scullytr on Stack Overflow See other posts from Stack Overflow or by scullytr
Published on 2010-03-24T18:39:24Z Indexed on 2010/03/24 18:43 UTC
Read the original article Hit count: 345

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!

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about jquery-selectors