We're looking for a user researcher to understand the needs of developers and designers. Is this you or someone you know? Check out the post: https://mzl.la/2IGzdXS



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


var 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. 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.)


<!--Sample HTML code-->

<div id="top" align="center">
  <div id="nested"></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) {
// Removing all children from an element
var element = document.getElementById("top");
while (element.firstChild) {


See also

Document Tags and Contributors

Last updated by: Potappo,