dutraveller
dutraveller

Reputation: 327

Wrap content with a div after each h2

Anyone can give me some ideas how can i wrap any content with a <div> after each <h2> tag and give it a unique id, done with jquery?

Im trying to do this:

<h2>Test</h2>
 <div id="wrap_1"> // This is the wrapper im trying to add
   <p>Bla bla</p>
   <p>More Bla bla</p>
 </div>
<h2>Another test</h2>
 <div id="wrap_2"> // This is the wrapper im trying to add
   <p>Bla bla</p>
   <p>More Bla bla</p>
 </div>

Thanks in advance

Upvotes: 1

Views: 748

Answers (1)

SLaks
SLaks

Reputation: 887797

Like this:

$('h2').each(function(index) { 
    $(this).nextUntil('h2').wrapAll('<div id="wrap' + index + '"></div>');
});

Demo

Upvotes: 6

Related Questions