THE WORLD'S LARGEST WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

XML DOM appendChild() Method


Node Object Reference Node Object

Example

The following code fragment loads "books.xml" into xmlDoc and creates a node (<edition>), and appends it after the last child of the first <book> node:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
        myFunction(xhttp);
    }
};
xhttp.open("GET", "books.xml", true);
xhttp.send();

function myFunction(xml) {
    var xmlDoc = xml.responseXML;
    var newel = xmlDoc.createElement("edition");
    var x = xmlDoc.getElementsByTagName("book")[0];
    x.appendChild(newel);
    document.getElementById("demo").innerHTML =
    x.getElementsByTagName("edition")[0].nodeName;
}

The output of the code above will be:

edition
Try it Yourself »

Definition and Usage

The appendChild() method appends the new child node to the end of the list of children of a node.

Note: If the newchild is already in the tree, it is first removed.


Browser Support

Internet Explorer Firefox Opera Google Chrome Safari

The appendChild() method is supported in all major browsers.


Syntax

nodeObject.appendChild(newchild)

Parameter Description
newchild The node to add (append)

Return Value

Type Description
Node object The appended node

Technical Details

DOM Version Core Level 1 Node Object. Modified in DOM Level 3

Try-It-Yourself Demos

appendChild() - Append a child node to all <book> nodes


Node Object Reference Node Object