"); } ``` ### Expected Output ```text A Notebook Harry Potter ``` --- ## Important Considerations 1. **Parent-Child Relationship:** The `old_node` must be a direct child of the node calling `replaceChild()`. If `old_node` is not a child of `parentNode`, a `DOMException` (specifically `NotFoundError`) will be thrown. 2. **Document Fragment Support:** If the `new_node` is a `DocumentFragment`, the `old_node` is replaced by all the children of the `DocumentFragment` in the same order. 3. **Memory Management:** The replaced `old_node` is still kept in memory if you have assigned it to a variable. It is removed from the active DOM tree, but you can still insert it elsewhere later or let it be garbage collected if no references to it remain.
Met Element Replacechild
## XML DOM: replaceChild() Method
The `replaceChild()` method replaces a specified child node of a parent element with a new node.
This method is a core part of the XML DOM (Document Object Model) and is widely used to dynamically update, modify, or swap elements within an XML or HTML document structure.
---
## Definition and Usage
The `replaceChild()` method is called on a parent node to replace one of its existing child nodes with a new node.
* **Return Value:** If the operation is successful, the method returns the replaced (old) node. If the operation fails, it returns `null` (or throws an exception depending on the environment).
* **Node Ownership:** If the `new_node` is already present in the document tree, it is first automatically removed from its original position before being inserted at the new position.
---
## Syntax
```javascript
parentNode.replaceChild(new_node, old_node);
```
### Parameters
| Parameter | Type | Description |
| :--- | :--- | :--- |
| `new_node` | Node | **Required.** The new node object to insert. |
| `old_node` | Node | **Required.** The existing child node object that you want to replace. |
---
## Code Example
The following example demonstrates how to load an XML document (`books.xml`) and replace the first `` element with a newly created `` element.
### Sample XML (`books.xml`)
Assume we have an XML file containing book records:
```xml
Everyday Italian
Harry Potter
```
### JavaScript Implementation
```javascript
// Load the XML document
var xmlDoc = loadXMLDoc("books.xml");
// Get the root element of the XML document
var rootElement = xmlDoc.documentElement;
// 1. Create the new book element and its child elements
var newBookNode = xmlDoc.createElement("book");
var newTitleNode = xmlDoc.createElement("title");
var newTextNode = xmlDoc.createTextNode("A Notebook");
// 2. Assemble the new node structure
newTitleNode.appendChild(newTextNode); // Append text to
newBookNode.appendChild(newTitleNode); // Append to
// 3. Locate the target node to be replaced (the first element)
var targetOldNode = xmlDoc.getElementsByTagName("book");
// 4. Replace the old book node with the new book node
rootElement.replaceChild(newBookNode, targetOldNode);
// 5. Output the titles of all books to verify the replacement
var titles = xmlDoc.getElementsByTagName("title");
for (var i = 0; i < titles.length; i++) {
document.write(titles.childNodes.nodeValue);
document.write("
"); } ``` ### Expected Output ```text A Notebook Harry Potter ``` --- ## Important Considerations 1. **Parent-Child Relationship:** The `old_node` must be a direct child of the node calling `replaceChild()`. If `old_node` is not a child of `parentNode`, a `DOMException` (specifically `NotFoundError`) will be thrown. 2. **Document Fragment Support:** If the `new_node` is a `DocumentFragment`, the `old_node` is replaced by all the children of the `DocumentFragment` in the same order. 3. **Memory Management:** The replaced `old_node` is still kept in memory if you have assigned it to a variable. It is removed from the active DOM tree, but you can still insert it elsewhere later or let it be garbage collected if no references to it remain.
"); } ``` ### Expected Output ```text A Notebook Harry Potter ``` --- ## Important Considerations 1. **Parent-Child Relationship:** The `old_node` must be a direct child of the node calling `replaceChild()`. If `old_node` is not a child of `parentNode`, a `DOMException` (specifically `NotFoundError`) will be thrown. 2. **Document Fragment Support:** If the `new_node` is a `DocumentFragment`, the `old_node` is replaced by all the children of the `DocumentFragment` in the same order. 3. **Memory Management:** The replaced `old_node` is still kept in memory if you have assigned it to a variable. It is removed from the active DOM tree, but you can still insert it elsewhere later or let it be garbage collected if no references to it remain.
YouTip