Reputation: 941
I have the following structure:
<div class='wrap1'>
<div class='element1'></div>
<div class='element2'></div>
</div>
Now I'd like to get:
<div class='wrap1'>
<div class='anotherWrap'><!-- this is new-->
<div class='element1'></div>
<div class='element2'></div>
</div>
</div>
That is, I need to wrap element1
and element2
in anotherWrap
.
I tried:
$('.wrap1').children().wrapAll("<div class='anotherWrap'></div>")
But, as I expected, this wrapped every sub div in my wrap1
div separately.
How to accomplish this?
Upvotes: 4
Views: 243
Reputation: 752
I was also trying to do but didn't find any solution. I used the following work around. Hope this will work for you..
$('.wrap1').prepend("<div class='anotherWrap'>").append("</div>");
Upvotes: 2
Reputation: 1547
3rd use .contents()
:
$('.wrap1').contents().wrapAll("<div class='anotherWrap'></div>");
Upvotes: 1
Reputation: 67217
Use .wrapInner()
over the parent element,
$('.wrap1').wrapInner("<div class='new' />");
Upvotes: 8
Reputation: 82251
Use:
$( ".element1,.element2" ).wrapAll( "<div class='anotherWrap'></div>");
Upvotes: 1