XML DOM removeChild() Method

Definition and Usage

removeChild() Method to remove child nodes.

The function returns the deleted node when successful, and returns NULL.

Syntax

elementNode.removeChild(node)
Parameter Description
node Required. Specifies the child node to be removed.

Instance

Example 1

The following code loads "books.xml" into xmlDoc and removes child nodes from 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 y = xmlDoc.getElementsByTagName("book")[0];
    var x = xmlDoc.documentElement.removeChild(y);
    document.getElementById("demo").innerHTML =
    "Removed node: " + x.nodeName;
}

Try It Yourself

Example 2

Remove the last child node from the node list:

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 len = xmlDoc.getElementsByTagName('book').length;
    var y = xmlDoc.getElementsByTagName("book")[len-1];
    var x = xmlDoc.documentElement.removeChild(y);
    document.getElementById("demo").innerHTML =
    "Removed node: " + x.nodeName;
}

Try It Yourself