2

I have the following;

<div class="content">
  <div class="row morepad">
    <a href="#">Association 1</a>
  </div>
  <div class="row">
   <a href="#">Association 2</a>
  </div>
  <div class="row morepad">
    <a href="#">Association 3</a>
  </div>
  <div class="row">
    <a href="#">Association 4</a>
  </div>
</div>

I am trying to wrap them into groups as the following separating according to the first morepad.

<div class="content">
 <div>
  <div class="row morepad">
    <a href="#">Association 1</a>
  </div>
  <div class="row">
   <a href="#">Association 2</a>
  </div>
 </div>
 <div>
  <div class="row morepad">
    <a href="#">Association 3</a>
  </div>
  <div class="row">
    <a href="#">Association 4</a>
  </div>
 </div>
</div>

I have tried the following:

$('.morepad').each(function() {
      $(this).nextUntil('.morepad').wrapAll('<div></div>');
  });
kayzoe
  • 61
  • 10
  • You need to include source morepad as well, Try `$(this).nextUntil('.morepad').addBack().wrapAll('
    ');` `.addBack()` or `.add(this)` should help.
    – PSL Jul 30 '14 at 19:46

1 Answers1

2

Jquery

$('.morepad').each(function() {
      $(this).nextUntil('.morepad').andSelf().wrapAll('<div></div>');
  });

you need to add addSelf so that it includes both the ends just wrapALl will only include the elements in betweeen

Working Jsfiddle link

Richie Fredicson
  • 2,150
  • 3
  • 16
  • 19