Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wrap elements when having their parent element?

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?

like image 651
Konrad Avatar asked Dec 15 '22 19:12

Konrad


2 Answers

Use .wrapInner() over the parent element,

$('.wrap1').wrapInner("<div class='new' />");

DEMO

like image 93
Rajaprabhu Aravindasamy Avatar answered Dec 17 '22 09:12

Rajaprabhu Aravindasamy


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>");
like image 24
Mohd. Shaukat Ali Avatar answered Dec 17 '22 09:12

Mohd. Shaukat Ali