Mozilla's getting a new look. What do you think? https://mzl.la/brandsurvey

The Node.removeChild() method removes a child node from the DOM. Returns removed node.

Syntax

var oldChild = node.removeChild(child);
OR
element.removeChild(child);
  • child is the child node to be removed from the DOM.
  • node 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.  With the first syntax-form shown, you may reuse the removed node later in your code, via the oldChild object reference.  In the second syntax-form however, there is no oldChild reference kept, so assuming your code has not kept any other reference to the node elsewhere, it will immediately become unusable and irretrievable, and will usually be automatically deleted from memory after a short time. 

If child is actually not a child of the element node, the method throws an exception. This will also happen if child was in fact a child of element at the time of the call, but was removed by an event handler invoked in the course of trying to remove the element (eg, blur.)

The method throws an exception in 2 different ways: 

  1. If the child was in fact a child of element and so existing on the DOM, but was removed the method throws the following exception:

    ​​Uncaught NotFoundError: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.

  2. If the child doesn't exist on the DOM of the page, the method throws the following exception:

    Uncaught TypeError: Failed to execute 'removeChild' on 'Node': parameter 1 is not of type 'Node'.

Examples

<!--Sample HTML code--> 
<div id="top" align="center"> </div>

<script type="text/javascript">
      var top = document.getElementById("top");
      var nested = document.getElementById("nested");

      var garbage = top.removeChild(nested);    //Test Case 2: the method throws the exception (2)

</script>

<!--Sample HTML code-->
<div id="top" align="center">
 <div id="nested"></div>
</div>

<script type="text/javascript">
      var top = document.getElementById("top");
      var nested = document.getElementById("nested");

      var garbage = top.removeChild(nested); // This first call remove correctly the node

      // ......
      garbage = top.removeChild(nested);   // Test Case 1: the method in the second call here, throws the exception (1)

</script>

<!--Sample HTML code-->

<div id="top" align="center">
  <div id="nested"></div>
</div>
// Removing a specified element when knowing its parent node
var d = document.getElementById("top");
var d_nested = document.getElementById("nested");
var throwawayNode = d.removeChild(d_nested);
// Removing a specified element without having to specify its parent node
var node = document.getElementById("nested");
if (node.parentNode) {
  node.parentNode.removeChild(node);
}
// Removing all children from an element
var element = document.getElementById("top");
while (element.firstChild) {
  element.removeChild(element.firstChild);
}

Specification

See also