Remove all the child elements of a DOM node in JavaScript
Child nodes can be removed from a parent with removeChild(), and a node itself can be removed with remove().
Another method to remove all child of a node is to set it’s innerHTML=”” property, it is an empty string which produces the same output. This method is not preferred to use.
Example-1: Using “removeChild()”.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <ul> <li>Get Up in Morning</li> <li>Do some exercise</li> <li>Get Ready for school</li> <li>Study Daily</li> <li>Do homework</li> </ul> <input id="btn" type="button" value="Remove Childrens"> </body> <script> function deleteChild() { var e = document.querySelector("ul"); //e.firstElementChild can be used. var child = e.lastElementChild; while (child) { e.removeChild(child); child = e.lastElementChild; } } var btn = document.getElementById( "btn").onclick = function() { deleteChild(); } </script> </html> |
Output:
Before Clicking on Button:

After Clicking on Button:

Example-2: Using innerHTML=”” property.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title> Document </title> </head> <body> <ul> <li>Get Up in Morning</li> <li>Do some exercise</li> <li>Get Ready for school</li> <li>Study Daily</li> <li>Do homework</li> </ul> <input id="btn" type="button" value="Remove Childrens"> </body> <script> function deleteChild() { var e = document.querySelector("ul"); e.innerHTML = ""; } var btn = document.getElementById( "btn").onclick = function() { deleteChild(); } </script> </html> |
Output:
Before Clicking on Button:

After Clicking on Button:

Example-3 Using “removeChild()”.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <ul> <li>Get Up in Morning</li> <li>Do some exercise</li> <li>Get Ready for school</li> <li>Study Daily</li> <li>Do homework</li> </ul> <input id="btn" type="button" value="Remove Childrens"> </body> <script> function deleteChild() { var e = document.querySelector("ul"); var first = e.firstElementChild; while (first) { first.remove(); first = e.firstElementChild; } } var btn = document.getElementById( "btn").onclick = function() { deleteChild(); } </script> </html> |
Output:
Before Clicking on Button:

After Clicking on Button:

Recommended Posts:
- JavaScript | Remove empty elements from an array
- Run Python script from Node.js using child process spawn() method
- How to select all child elements recursively using CSS?
- How to get specific number of child elements using CSS?
- How to remove CSS property using JavaScript?
- How to remove text from a string in JavaScript?
- How to remove an HTML element using JavaScript ?
- How to remove a character from string in JavaScript ?
- How to remove time from date using JavaScript ?
- How to remove an element from an array in JavaScript?
- JavaScript | Remove a JSON attribute
- JavaScript | Remove the last item from an array
- How to remove a property from JavaScript object ?
- How to remove portion of a string after certain character in JavaScript ?
- How to remove style added with .css() function using JavaScript?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



