XML DOM replaceChild()Methods


Element object

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

elementNode.replaceChild(new_node,old_node)

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


Example

The following code snippet usesloadXMLDoc()put "books.xmlLoad into xmlDoc, and replace the first <book> element:

Example

xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.documentElement; //Create the book element, title element, and text node newNode=xmlDoc.createElement("book"); newTitle=xmlDoc.createElement("title"); newText=xmlDoc.createTextNode("A Notebook"); //Add a text node to the title node newTitle.appendChild(newText); //Add the title node to the book node newNode.appendChild(newTitle); y=xmlDoc.getElementsByTagName("book")[0] //Replace the first book node with a new node x.replaceChild(newNode,y); z=xmlDoc.getElementsByTagName("title"); for (i=0;i<z.length;i++) { document.write(z[i].childNodes[0].nodeValue); document.write("<br>"); }

Try it Yourself »

Element objectother extensions