XML DOM removeChild() Method

Definition and Usage

removeChild() This method removes the specified child node from the current node.

Tip:The deleted child node can be inserted into any element in the same document later. Please use the insertBefore() or appendChild() method to insert it into the same document later, or use the adoptNode() or importNode() method to insert the deleted node into another document.

Syntax

nodeObject.removeChild(child)

Parameter

Parameter Description
child Required. Node object. The node to be deleted.

Technical Details

DOM Version: Core Level 1 Node Object. Modified in DOM Level 3.
Return value: Node object. The node to be deleted is returned as a Node object.

Example

The following code loads "books.xml" into xmlDoc and deletes the first child node of the first <book> element:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
   if (this.readyState == 4 && this.status == 200) {
       myFunction(this);
   }
};
xhttp.open("GET", "books.xml", true);
xhttp.send();
function myFunction(xml) {
    var xmlDoc = xml.responseXML;
    var root = xmlDoc.documentElement;
    var currNode = root.childNodes[1];
    removedNode = currNode.removeChild(currNode.childNodes[1]);
    document.getElementById("demo").innerHTML =
    "Removed node: " + removedNode.nodeName;
}

Try It Yourself

Browser Support

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
Support Support Support Support Support

All mainstream browsers support removeChild() Methods.