Visit Mozilla.org

DOM:element.removeChild

From MDC

(Redirected from removeChild)

« Gecko DOM Reference

Contents

[edit] Summary

Removes a child node from the DOM.

[edit] Syntax

oldChild = element.removeChild(child) 
  • child is the child node to be removed from the DOM.
  • element is the parent node of child.
  • oldChild holds a reference to the removed child node. oldChild == child.

The removed child node still exists in memory, but is no longer part of the DOM. You may reuse the removed node later in your code, via the oldChild object reference.

If child is actually not a child of the element node, the method throws an exception.

[edit] Example

// <div id="top" align="center">
//   <div id="nested"></div> 
// </div>

var d = document.getElementById("top"); 
var d_nested = document.getElementById("nested"); 
var throwawayNode = d.removeChild(d_nested);
// remove all children from element
var element = document.getElementById("top");
while (element.firstChild) {
  element.removeChild(element.firstChild);
}

[edit] Specification