Removing Nodes from the DOM

Welcome to our free JavaScript tutorial. This tutorial is based on Webucator's Introduction to JavaScript Training course.

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.parentNode.removeChild(elem);
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()) {
	removeElement(parent.childNodes[0]);
}
Next

This tutorial is based on Webucator's Introduction to JavaScript Training Course. We also offer many other JavaScript Training courses. Sign up today to get help from a live instructor.