5

I would like to remove one div element, but without its children. For example, let's say that i have one div with id wrapper, and inside it 5 paragraphs.

I want to remove only the wrapper div, but to leave paragraphs alive. I have tried both remove() and detach(), but they both clean out the inner elements.

Any advice?

gnat
  • 6,213
  • 108
  • 53
  • 73
fluxus
  • 559
  • 5
  • 15
  • 29
  • 1
    Older question, probably with obsolete answers: http://stackoverflow.com/questions/2032317/how-can-i-remove-a-surrounding-div-with-jquery – Robert Harvey Jun 28 '12 at 21:10

4 Answers4

14

http://api.jquery.com/unwrap/ should do it:

The .unwrap() method removes the element's parent. This is effectively the inverse of the .wrap() method. The matched elements (and their siblings, if any) replace their parents within the DOM structure...

gnat
  • 6,213
  • 108
  • 53
  • 73
Uku Loskit
  • 40,868
  • 9
  • 92
  • 93
6

jsFiddle demo

$('#element').contents().unwrap();
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
4

Check out .replaceWith()

$('#theDiv').replaceWith($('#theDiv').contents());
Steve Robbins
  • 13,672
  • 12
  • 76
  • 124
  • You are the saviour. I'm using one CMS, which tends to add wrapper div to any content added through HTML editor. But it messed up my absolute positioned elements, so i needed to remove it. – fluxus Jun 28 '12 at 21:12
0
$('#yourdivIDtoremove').replaceWith($(this).text());

should do ;)

sachleen
  • 30,730
  • 8
  • 78
  • 73