0

I have HTML like :

<div id="parent">
 <ul class="list">something here....</ul>
</div>

When page load, I want kill div that have id="parent". That means after finish the loading, I have only :

<ul class="list">something here....</ul>

How Javascript or Jquery can do this? Thanks for your help.

Hai Tien
  • 2,929
  • 7
  • 36
  • 55

5 Answers5

4

Can you try:

$(function() {
    $("ul.list").unwrap();
});

Docs for unwrap().

Nikos Paraskevopoulos
  • 39,514
  • 12
  • 85
  • 90
2

Try this famous code to completely remove div:

document.addEventListener('DOMContentLoaded',function(){
    var element = document.getElementById("parent");
    element.parentNode.removeChild(element);
},false);

remove element by id

And to remove only parent div use jQuery's unwrap:

$(function() {
    $("ul.list").unwrap();
});

OR in plain javascript:

  function unwrap() {
            var a = document.getElementById('element-to-be-unwrapped');
            newelement = a.firstElementChild.cloneNode();
            document.body.insertBefore(newelement, a);
            a.parentNode.removeChild(a);
  }
Community
  • 1
  • 1
Vicky Gonsalves
  • 11,593
  • 2
  • 37
  • 58
1

in jQuery, here is the code:

var ul_holder = $('#parent').html();
$('#parent').remove();
$(document).append(ul_holder);

You can replace $(document) with any other element

matewka
  • 9,912
  • 2
  • 32
  • 43
Alaa Badran
  • 1,848
  • 15
  • 19
1

Use .unwrap() function

  if (  $(".list").parent('#parent').is( "div" ) ) {
     $(".list").unwrap();
  }
Nishu Tayal
  • 20,106
  • 8
  • 49
  • 101
1

You can use closest() with remove()

$(function() {
    $("ul.list").closest('#parent').remove();
});

or you can use unwrap() like,

$(function() {
    $("ul.list").unwrap();
});
Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106