Remove All children πŸ‘Ά of the node in Javascript.

Learn how to delete all the nodes inside a div or any node in Javascript.


You can do it in three ways

1Β . Set the node innerHTML as empty-string(β€œβ€).

var node = document.getElementById('parent');
node.innerHTML = "";

This method removes all the text and nodes inside the parent node. If you need to delete only the nodes then you can go for second method.

2. Second way is to remove firstChild of the parent node until the node has a childrenΒ ,

var node= document.getElementById("parent");
while (node.firstChild) {
    node.removeChild(myNode.firstChild);
}

3. You can also use node.remove() method to delete the nodes

var node= document.getElementById("parent");
node.querySelectorAll('*').forEach(n => n.remove());

If you’re using jQuery then we can empty the parent by $(node).empty()

If you find this helpful surprise 🎁 me here.

Share if you feel happy.

Follow Javascript JeepπŸš™ if you feel worthy.

Leave a comment

Design a site like this with WordPress.com
Get started