

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can I remove all child elements of a DOM node in JavaScript?
To remove the child elements of <div>
we can use,
var list = document.getElementById("mList"); while (list.hasChildNodes()) { list.removeChild(list.firstChild); } }
It will remove the all child of
which id is 'mList'.
Example
In your code it can be written as -
<html> <body> <div id="mList" style="width:400px;background-color:gray"> <ul> <li>li- child</li> <li>li- child</li> </ul> </div> <button onclick="mFunction()">Submit</button> <script> function mFunction() { var list = document.getElementById("mList"); while (list.hasChildNodes()) { list.removeChild(list.firstChild); } } </script> </body> </html>
- Related Questions & Answers
- Remove all child elements of a DOM node in JavaScript?
- How can I remove a child node in HTML using JavaScript?
- Remove the child node of a specific element in JavaScript?
- How to remove all child nodes from a parent node using jQuery?
- First and last child node of a specific node in JavaScript?
- Child node count in JavaScript?
- Replace a child node with a new node in JavaScript?
- How can I remove all elements except the first one using jQuery?
- Insert a node as a child before an existing child in JavaScript?
- How do I remove all elements from a list in Java?
- How to remove all child nodes from a parent in jQuery?
- How to remove all the elements from a map in JavaScript?
- How to remove all the elements from a set in javascript?
- How can I remove the decimal part of JavaScript number?
- How can I update child objects in MongoDB?
Advertisements