English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

HTML DOM removeChild() Method

HTML DOM Element Object

removeChild()The method removes a child from the DOM and returns the removed node.

Syntax:

node.removeChild(node)
var div = document.getElementById("demo");
div.removeChild(div.firstElementChild);
Test and See‹/›

Browser Compatibility

All browsers fully support the removeChild() method:

Method
removeChild()IsIsIsIsIs

Parameter Value

ParameterDescription
nodeThe node removed from the given parent node (usually an element)

Technical Details

Return value:The returned value is the deleted child
DOM Version:DOM Level1

More examples

Check if the DIV has any child nodes. If found, delete its first child element (index 0):

var div = document.getElementById("demo");
if (div.hasChildNodes()) {
   div.removeChild(div.children[0]);
}
Test and See‹/›

Remove all child elements from an element:

var div = document.getElementById("demo");
while (div.hasChildNodes()) {
 div.removeChild(div.children[0]);
}
Test and See‹/›

Remove a specified element without specifying its parent node:

var node = document.getElementById("myP");
if (node.parentNode) {
   node.parentNode.removeChild(node);
}
Test and See‹/›

Related References

HTML DOM Reference:node .parentNode() method

HTML DOM Reference:node .hasChildNodes() method

HTML DOM Reference:node .appendChild() method

HTML DOM Reference:node .insertBefore() method

HTML DOM Reference:node .replaceChild() method

HTML DOM Element Object