| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/en-US/docs/Web/API/Node/removeChild | [Back] [Original] |
Get to know MDN better
This feature is well established and works across many devices and browser versions. Its been available across browsers since July 2015.
The removeChild() method of the Node interface
removes a child node from the DOM and returns the removed node.
Note: As long as a reference is kept on the removed child, it still exists in memory, but is no longer part of the DOM. It can still be reused later in the code.
If the return value of removeChild() is not stored, and no other reference is kept,
it will be automatically deleted from memory after a short time.
Unlike Node.cloneNode() the return value preserves the EventListener objects associated with it.
removeChild(child)
The removed child node.
NotFoundError DOMExceptionThrown if the child is not a child of the node.
TypeErrorThrown if the child is null.
Given this HTML:
<div id="parent">
<div id="child"></div>
</div>
To remove a specified element when knowing its parent node:
const parent = document.getElementById("parent");
const child = document.getElementById("child");
const throwawayNode = parent.removeChild(child);
To remove a specified element without having to specify its parent node:
const node = document.getElementById("child");
if (node.parentNode) {
node.parentNode.removeChild(node);
}
To remove all children from an element:
const element = document.getElementById("idOfParent");
while (element.firstChild) {
element.removeChild(element.firstChild);
}
<!--Sample HTML code-->
<div id="parent"></div>
const parent = document.getElementById("parent");
const child = document.getElementById("child");
// Throws Uncaught TypeError
const garbage = parent.removeChild(child);
<!--Sample HTML code-->
<div id="parent">
<div id="child"></div>
</div>
const parent = document.getElementById("parent");
const child = document.getElementById("child");
// This first call correctly removes the node
const garbage = parent.removeChild(child);
// Second call throws NotFoundError
parent.removeChild(child);
| Specification |
|---|
| DOM # dom-node-removechild |
This page was last modified on Jun 26, 2025 by MDN contributors.
NodeAbortControllerAbortSignalAbstractRangeAttrCDATASectionCharacterDataCommentCustomEventDOMErrorDOMExceptionDOMImplementationDOMParserDOMTokenListDocumentDocumentFragmentDocumentTypeElementEventEventTargetHTMLCollectionMutationObserverMutationRecordNamedNodeMapNodeIteratorNodeListProcessingInstructionQuotaExceededErrorRangeShadowRootStaticRangeTextTreeWalkerXMLDocumentXPathEvaluatorXPathExpressionXPathResultXSLTProcessorYour blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |