Removing Nodes from the DOM

Contact Us or call 1-877-932-8228
Removing Nodes from the DOM

Removing Nodes from the DOM

Element nodes have a removeChild() method, which takes a single parameter: the child node to be removed. There is no W3C method for a node to remove itself, but the following code will do the trick:

elem = null;

removeChild(elem) only removes the element from its parent. Setting elem to null destroys the element.

Sometimes it's useful to remove all of a node's children in one fell swoop. The code below will handle this:

while (parent.hasChildNodes()) {