This page is Ready to Use

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

removeChild

Summary

Removes a child node from a node.

Method of dom/Nodedom/Node

Syntax

var removedNode = node.removeChild(/* see parameter list */);

Parameters

oldChild

Data-type
Blob

The node to be removed from the document.

Return Value

Returns an object of type DOM NodeDOM Node

The removed node.

Examples

This example uses the removeChild method to remove a bold element from a div.

<!doctype html>
<html>
<head>
<script type="application/javascript">
function removeElement()
{
  var div1 = document.getElementById("Div1");
  try
  {
      //The first child of the div is the bold element.
    var oChild=div1.children(0);
    div1.removeChild(oChild);
  }
  catch(x)
  {
    alert("You have already removed the bold element.\nPage will be refreshed when you click OK.")
    document.location.reload();
  }
}
</script>
</head>
<body>
<div id="Div1" onclick="removeElement()">
Click anywhere in this sentence to remove this <strong>Bold</strong> word.
</div>
</body>
</html>

Remove all children from a node.

while (element.lastChild) {
  element.removeChild(element.lastChild);
}

Notes

The node to be removed must be an immediate child of the parent node. This method is accessible at run time. If elements are removed at run time, before the closing tag is parsed, areas of the document might not render.

Related specifications

DOM Level 3 Core
Recommendation

Attributions