Node.removeChild

  • Revision slug: DOM/Node.removeChild
  • Revision title: Node.removeChild
  • Revision id: 10430
  • Created:
  • Creator: geoffschmidt
  • Is current revision? No
  • Comment 42 words added

Revision Content

{{ DomRef() }}

Summary

Removes a child node from the DOM. Returns removed node.

Syntax

var oldChild = element.removeChild(child);
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. 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.)

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);
}

Specification

{{ languages( { "fr": "fr/DOM/element.removeChild", "ja": "ja/DOM/element.removeChild", "pl": "pl/DOM/element.removeChild" } ) }}

Revision Source

<p>{{ DomRef() }}</p>
<h3 name="Summary">Summary</h3>
<p>Removes a child node from the DOM. Returns removed node.</p>
<h3 name="Syntax">Syntax</h3>
<pre class="eval"><em>var oldChild</em> = <em>element</em>.removeChild(<em>child</em>);
<em>element</em>.removeChild(<em>child</em>);
</pre>
<ul> <li><code>child</code> is the child node to be removed from the DOM.</li> <li><code>element</code> is the parent node of <code>child</code>.</li> <li><code>oldChild</code> holds a reference to the removed child node. <code>oldChild</code> == <code>child</code>.</li>
</ul>
<p>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 <code>oldChild</code> object reference.</p>
<p>If <code>child</code> is actually not a child of the <code>element</code> node, the method throws an exception. This will also happen if <code>child</code> was in fact a child of <code>element</code> 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.)</p>
<h3 name="Example">Example</h3>
<pre>// &lt;div id="top" align="center"&gt;
//   &lt;div id="nested"&gt;&lt;/div&gt; 
// &lt;/div&gt;

var d = document.getElementById("top"); 
var d_nested = document.getElementById("nested"); 
var throwawayNode = d.removeChild(d_nested);
</pre>
<pre>// remove all children from element
var element = document.getElementById("top");
while (element.firstChild) {
  element.removeChild(element.firstChild);
}
</pre>
<h3 name="Specification">Specification</h3>
<ul> <li><a class="external" href="http://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html#method-removeChild">DOM Level 1 Core: removeChild</a></li> <li><a class="external" href="http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-1734834066">DOM Level 2 Core: removeChild</a></li>
</ul>
<p>{{ languages( { "fr": "fr/DOM/element.removeChild", "ja": "ja/DOM/element.removeChild", "pl": "pl/DOM/element.removeChild" } ) }}</p>
Revert to this revision