The replaceChild() method replaces a child node with a new node.
The new node could be an existing node in the document, or you can create a new node.
Tip: The replaced child node can be inserted later into any element in the same document. Use the insertBefore() or appendChild() method to insert it later into the same document, or use the adoptNode() or importNode() method to insert the replaced node into another document.
The replaceChild() method is supported in all major browsers.
Parameter | Type | Description |
---|---|---|
newchild | Node object | Required. The new node to put in the child list |
oldchild | Node object | Required. The node that will be replaced in the child list |
Type | Description |
---|---|
Node object | The replaced node (oldchild) |
DOM Version | Core Level 1 Node Object. Modified in DOM Level 3 |
---|
The following code fragment loads "books.xml" into xmlDoc using loadXMLDoc() and replaces the first <book> node with a new book node: