JQuery Wrap a div from H2 to the last paragraph

I want to wrap <div> FROM THE BEGINNING <H2> to the next <H2> , but it only starts with the first paragraph.

This is what I have that ALMOST does this work:

 $('h2:contains("test")').nextUntil('h2').wrapAll('<div class="wrapper" />'); 

Here is my HTML:

 /* from here */ <h2 class='test'>Include this in the wrap</h2> <p>this</p> <p>and this</p> <p>and this</p> /* to here */ <h2 class='next'>before this</h2> 
+2
source share
1 answer

I would try:

 $("h2.test").nextUntil("h2").andSelf().wrapAll('<div class="wrapper" />'); 

This seems like a trick.

+4
source

All Articles