Easy Tutorial
❮ Met Element Setattributens Dom Met Element Getattribute ❯

XML DOM replaceChild() Method



Definition and Usage

The replaceChild() method replaces a child node with another node.

If successful, the function returns the replaced node; if it fails, it returns NULL.

Syntax

Parameter Description
new_node Required. Specifies the new node.
old_node Required. Specifies the child node to be replaced.

Example

The following code snippet uses loadXMLDoc() to load "books.xml" into xmlDoc and replaces the first <book> element:

Example

xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.documentElement;

// Create book element, title element, and text node
newNode=xmlDoc.createElement("book");
newTitle=xmlDoc.createElement("title");
newText=xmlDoc.createTextNode("A Notebook");

// Append text node to title node
newTitle.appendChild(newText);
// Append title node to book node
newNode.appendChild(newTitle);

y=xmlDoc.getElementsByTagName("book")[0]
// Replace the first book node with the new node
x.replaceChild(newNode,y);

z=xmlDoc.getElementsByTagName("title");
for (i=0;i&lt;z.length;i++)
{
    document.write(z[i].childNodes[0].nodeValue);
    document.write("<br>");
}

❮ Met Element Setattributens Dom Met Element Getattribute ❯