Node.removeChild()
The Node.removeChild() method removes a child node from the DOM and returns the removed node.
Syntax
var oldChild = node.removeChild(child);
Or just:
node.removeChild(child);
-
childis the child node to be removed from the DOM. -
nodeis the parent node ofchild. -
oldChildholds a reference to the removed child node, i.e.,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 (e.g., blur.)
Exception
-
NotFoundErrorDOMException -
Thrown if the
childwas in fact a child ofelementand so existing on the DOM, but was removed. -
TypeErrorDOMException -
Thrown if the
childdoesn't exist on the DOM of the page.
Examples
Simple examples
Given this HTML:
<div id="top"> <div id="nested"></div> </div>
To remove a specified element when knowing its parent node:
let d = document.getElementById("top"); let d_nested = document.getElementById("nested"); let throwawayNode = d.removeChild(d_nested);
To remove a specified element without having to specify its parent node:
let node = document.getElementById("nested"); if (node.parentNode) { node.parentNode.removeChild(node); }
To remove all children from an element:
let element = document.getElementById("top"); while (element.firstChild) { element.removeChild(element.firstChild); }
Causing a TypeError
<!--Sample HTML code--> <div id="top"> </div> <script type="text/javascript"> let top = document.getElementById("top"); let nested = document.getElementById("nested"); // Throws Uncaught TypeError let garbage = top.removeChild(nested); </script>
Causing a NotFoundError
<!--Sample HTML code--> <div id="top"> <div id="nested"></div> </div> <script type="text/javascript"> let top = document.getElementById("top"); let nested = document.getElementById("nested"); // This first call correctly removes the node let garbage = top.removeChild(nested); // Throws Uncaught NotFoundError garbage = top.removeChild(nested); </script>
Notes
Unlike Node.cloneNode() the return value preserves the EventListeners.
Specifications
| Specification |
|---|
| DOM Standard (DOM) # dom-node-removechild |
Browser compatibility
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
removeChild |
1 |
12 |
1 |
5 |
7 |
1.1 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
See also
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Node/removeChild