DOM Node.cloneNode() Method
The cloneNode() method makes a copy of a node, optionally including all of its children.
Description
The cloneNode() method returns a copy of the node. With deep set to true, the whole subtree is copied too. Attributes are copied, but event listeners added with addEventListener() are not. Watch for duplicate id values in clones. It's commonly used to stamp out copies of a <template>'s content.
Without deep, only the node itself is copied: for an element that means its tag and attributes, but none of its content or children. Form controls copy their attribute values, but some current state, such as text typed into an input, may not be copied the same way in every browser, so set values explicitly on the clone if they matter.
The clone isn't part of any document until you insert it, and inline onclick attributes are copied because they're attributes. For copying elements from another document, such as a parsed template, document.importNode() is the equivalent method.
Syntax
node.cloneNode()
node.cloneNode(deep)Parameters
- deepOptional.
trueto copy all descendants as well. Defaults tofalse.
Return Value
The new copy of the node, which is not yet in the document.
Example
Browser Support
The following information will show you the current browser support for the Node.cloneNode() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





