Sunil Chaudhry Sunil Chaudhry - 1 year ago 65
jQuery Question

How to add a parent div for existing divs

I have 2 divs(dynamically created) , I want to add a parent div for these existing divs.


<div id="a">div1</div>
<div id="b">div2</div>

Output should be like this :-

<div id="1">Main Div
<div id="a">div1</div>
<div id="b">div2</div>

Answer Source

Use .wrapAll() - which wrap an HTML structure around all elements in the set of matched elements.

$( ".inner" ).wrapAll( "<div id='1' />");
<script src=""></script>
<div class="inner" id="a">div 1</div> 
<div class="inner" id="b">div 2</div> 

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download