Node.cloneNode()
The Node.cloneNode() method returns a duplicate of the node on which this method was called.
Syntax
let newClone = node.cloneNode([deep])
node-
The node to be cloned.
newClone-
The new node, cloned from
node.The
newClonehas no parent and is not part of the document, until it is added to another node that is part of the document (usingNode.appendChild()or a similar method). -
deepOptional -
If
true, thennodeand its whole subtree—including text that may be in childTextnodes—is also copied.If
false, onlynodewill be cloned. Any text thatnodecontains is not cloned, either (since text is contained by one or more childTextnodes).deephas no effect on empty elements (such as the<img>and<input>elements).
Example
let p = document.getElementById("para1") let p_prime = p.cloneNode(true)
Notes
Cloning a node copies all of its attributes and their values, including intrinsic (inline) listeners. It does not copy event listeners added using addEventListener() or those assigned to element properties (e.g., node.onclick = someFunction). Additionally, for a <canvas> element, the painted image is not copied.
Warning: cloneNode() may lead to duplicate element IDs in a document!
If the original node has an id attribute, and the clone will be placed in the same document, then you should modify the clone's ID to be unique.
Also, name attributes may need to be modified also, depending on whether duplicate names are expected.
To clone a node to insert into a different document, use Document.importNode() instead.
Specifications
| Specification |
|---|
| DOM Standard (DOM) # ref-for-dom-node-clonenode① |
Browser compatibility
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
cloneNode |
1 |
12 |
1 |
6 |
7 |
1.1 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Node/cloneNode