Tuesday, June 4, 2024
 Popular · Latest · Hot · Upcoming
136
rated 0 times [  138] [ 2]  / answers: 1 / hits: 28941  / 13 Years ago, tue, december 20, 2011, 12:00:00

I am learning the basics of javascript. now it DOM and i am stuck here, How can i remove a parent node with all its chldren. eg say i have the html code like this.



<ul id =parent>
<li>Hi</li>
<li>How</li>
<li>Are</li>
<li>You</li>
</ul>


i wand to delete the <ul> element with all its child <li> nodes. I tried it to do like this document.getElementById('parent').removeNode(true); but its not working. Can anyone help me here. ?


More From » javascript

 Answers
39

You need to get a handle to the ul element then ask its parent element to delete it by passing the ul handle the parent's removeChild method.



This is how you would do it without the jQuery framework:



var x = document.getElementById('parent');
x.parentNode.removeChild(x);


Of course if you used jQuery it would be simple like this:



$(#parent).remove();

[#88471] Sunday, December 18, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
halleyb

Total Points: 604
Total Questions: 96
Total Answers: 115

Location: Tokelau
Member since Wed, Oct 14, 2020
4 Years ago
;