XML DOM cloneNode() Method
❮ Node Object
Example
The following code fragment loads "books.xml", clones the first <book> node and then adds it to the end of the node list:
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xhttp.open("GET", "books.xml", true);
xhttp.send();
function myFunction(xml) {
var x, y, cloneNode, i, xmlDoc, txt;
xmlDoc = xml.responseXML;
txt = "";
x = xmlDoc.getElementsByTagName('book')[0];
cloneNode
= x.cloneNode(true);
xmlDoc.documentElement.appendChild(cloneNode);
//
Output all titles
y = xmlDoc.getElementsByTagName("title");
for (i = 0; i < y.length; i++) {
txt += y[i].childNodes[0].nodeValue + "<br>";
}
document.getElementById("demo").innerHTML = txt;
}
The output of the code above will be:
Everyday Italian
Harry Potter
XQuery Kick Start
Learning XML
Everyday Italian
Try it Yourself »
Definition and Usage
The cloneNode() method creates a copy of a node, and returns the newly clone node.
Browser Support
The cloneNode() method is supported in all major browsers.
Syntax
nodeObject.cloneNode(deep)
Parameters
Parameter | Type | Description |
---|---|---|
deep | Boolean | true clones the node, its attributes, and its descendants.
false clones the node and its attributes. |
Return Value
Type | Description |
---|---|
Node object | The cloned node |
Technical Details
DOM Version | Core Level 1 Node Object |
---|
❮ Node Object