<div id="parent">
<div id="child"></div>
</div>Node: removeChild() method / notebook
Node: removeChild() method
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
const parent = document.getElementById("parent");
const child = document.getElementById("child");
const throwawayNode = parent.removeChild(child);const node = document.getElementById("child");
if (node.parentNode) {
node.parentNode.removeChild(node);
}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);