Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove wrapping div and leave all sub-divs intact?

Tags:

jquery

I have one wrapper div with several sub-divs inside and tags inside those sub-divs as well. I want to remove the wrapper div. I have considered JQuery's unwrap, but it appears as though I need to specify the child divs to tell Jquery what to unwrap. Will this work if there are several children?

So, the code looks like:

<div id="wrapper">
  <div id="innerDiv1"></div>
  <div id="innerDiv2"></div>
  <div id="innerDiv3"></div>
</div>

Any help, as always, is appreciated.

Thank you.

like image 871
robert smith Avatar asked Feb 21 '12 22:02

robert smith


2 Answers

The unwrap method will work fine (you can select any of/any number of the siblings):

$("#innerDiv1").unwrap();

From the docs (emphasis added):

The matched elements (and their siblings, if any) replace their parents within the DOM structure.

like image 75
James Allardice Avatar answered Nov 15 '22 20:11

James Allardice


To add on to @james

You can do something like this

var innerDivs = $("#wrapper").html();
$("#wrapper").remove();
$("body").append(innerDivs);​ // you will need to change this to append to whatever element you like

jsfiddle example

http://jsfiddle.net/dAZ9D/

like image 24
Jaspreet Chahal Avatar answered Nov 15 '22 20:11

Jaspreet Chahal