0

Possible Duplicate:
Wrap every 3 divs in a div
jQuery - use wrap() to wrap multiple elements?

Lets say I have 4 divs, as below:

<div class="section">1</div>
<div class="section">2</div>
<div class="section">3</div>
<div class="section">4</div>

I would like to use $('.section' [increments of 2] ).wrap('<div class="row"></div>') so each 2 div.section's will be wrapped with div.row, so the end result would look like this:

<div class="row">
  <div class="section">1</div>
  <div class="section">2</div>
</div>

<div class="row">
  <div class="section">3</div>
  <div class="section">4</div>
</div>

How is this done?

Community
  • 1
  • 1
Control Freak
  • 12,965
  • 30
  • 94
  • 145
  • Exactly the same question http://stackoverflow.com/questions/3475594/jquery-use-wrap-to-wrap-multiple-elements and http://stackoverflow.com/questions/1432201/how-to-wrap-every-3-child-divs-with-html-using-jquery – Cheery Feb 18 '12 at 06:56

1 Answers1

1
$('.section:even').each(function(){
        $(this).next().andSelf().wrapAll('<div class="row">');
});

Based on jQuery - use wrap() to wrap multiple elements?

Community
  • 1
  • 1
Cheery
  • 16,063
  • 42
  • 57