Remove All children 👶 of the node in Javascript.

Remove All children 👶 of the node in Javascript.

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

Image for post

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.

26