The Node.cloneNode() method returns a duplicate of a node. Learn deep vs shallow cloning, what is and is not copied, the duplicate-id warning, and when to prefer document.importNode().
01
Kind
Instance method
02
Returns
New Node copy
03
deep
true = subtree
04
Listeners
Attributes only
05
In document?
Not until append
06
Status
Baseline widely
Fundamentals
Introduction
When you need another copy of a DOM node instead of moving the original, cloneNode() is the tool. Pair it with appendChild() to insert the copy.
Cloning copies attributes by default. It does not copy addEventListener handlers or paint data for <canvas>. Always watch for duplicate id values when both nodes stay in the same document.
💡
Beginner tip
Pass true for a deep clone (children included). Omit it or pass false for a shallow clone (element shell only).
Concept
Understanding cloneNode()
MDN: cloneNode() returns a duplicate of the node. The optional deep flag controls whether the subtree is copied too.
Attributes — copied, including inline event attributes.
Subtree — copied only when deep is true.
Not copied — addEventListener / onevent properties; canvas paint.
Parent — clone starts detached (parentNode is null).
Node.cloneNode() is Baseline Widely available across modern browsers (MDN: since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline · Widely available
Node.cloneNode()
Safe for production. Use deep true for full copies; fix ids; re-bind addEventListener handlers.
UniversalWidely available
Google ChromeFull support · Desktop & Mobile
Full support
Mozilla FirefoxFull support · Desktop & Mobile
Full support
Apple SafariFull support · macOS & iOS
Full support
Microsoft EdgeFull support · Chromium & Legacy
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerLong-standing support in legacy IE
Full support
cloneNode()Excellent
Bottom line: Deep-clone when you need children; shallow for the shell; importNode for other documents / template custom elements.
Wrap Up
Conclusion
Node.cloneNode() duplicates a node. Choose deep or shallow, fix ids, remember listeners are not fully copied, and append the detached clone when you are ready.
Prefer importNode for templates with custom elements
Append the clone—it starts detached
❌ Don’t
Leave duplicate ids in the same document
Assume JS listeners were copied
Expect canvas drawings to come along
Confuse cloning with moving via appendChild
Confuse DOM Node with the Node.js runtime
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about cloneNode()
Duplicate a node; deep controls the subtree.
5
Core concepts
📋01
Returns a copy
detached Node
API
🌳02
deep true
copies subtree
Flag
🚩03
Fix ids
avoid duplicates
Gotcha
🔒04
Listeners
attrs only
Events
📦05
importNode
other documents
Alt
❓ Frequently Asked Questions
It returns a duplicate of the node. With deep true, the whole subtree is copied; with false or omitted, only the node itself is cloned (no children).
No. MDN marks Node.cloneNode() as Baseline Widely available (since July 2015). It is a standard DOM method — not Deprecated, Experimental, or Non-standard.
It copies attributes, including inline handlers like onclick="...". It does not copy listeners added with addEventListener() or assigned via onevent properties such as node.onclick = fn.
If the original has an id and you insert the clone into the same document, you get two elements with the same id. Change the clone’s id (and sometimes name) to stay unique.
Use document.importNode() when cloning into another document, or when cloning HTML template content that may include custom elements that should upgrade in the current document.
No. The clone has no parent until you insert it with appendChild, insertBefore, or a similar method.
Did you know?
MDN notes that deep has no effect on void elements such as <img> and <input>—they have no children to clone either way.