jquerywrapallnextuntil

Wrap group of divs until next class


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>');
  });

Solution

  • 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