How to use .wrapAll () in jQuery?
I need to find all p tags inside everything div
with a class someClass
and wrap them in another div
. This is what the initial markup should look like:
<div class="someClass">
// Lots of different tags generated by the site
<p>Some text</p>
<p>Some text</p>
<p>Some text</p>
<p>Some text</p>
</div>
<div class="someClass">
// Lots of different tags generated by the site
<p>Some text</p>
<p>Some text</p>
</div>
Would turn into:
<div class="someClass">
// Lots of different tags generated by the site
<div class="bla">
<p>Some text</p>
<p>Some text</p>
<p>Some text</p>
<p>Some text</p>
</div>
</div>
<div class="someClass">
// Lots of different tags generated by the site
<div class="bla">
<p>Some text</p>
<p>Some text</p>
</div>
</div>
Any ideas? When I try to use .each()
: for each div
with a class, someClass
wrap all the tags p
, but it will just concatenate them all together at the top div
.
source to share
Have you tried this?
$('div.someClass p').wrapAll(...);
Or that?
$('div.someClass').each(function() {
$(this).find('p').wrapAll(...);
});
Edit
After looking at the code you posted it is a syntax problem. You need quotes on this line:
$(this).find('p').wrapAll(<div class='toggle'></div>);
It should be:
$(this).find('p').wrapAll("<div class='toggle'></div>");
source to share