node.cloneNode(deep) returns a duplicate of the node(MDN). Pass true to copy the whole subtree; omit or pass false for a shallow copy.
Status: Baseline Widely available (since July 2015) — not Deprecated, Experimental, or Non-standard. Warning: clones can duplicate id values — rename before inserting.
Remember
node.cloneNode() → shallow (node only; default deep=false)
node.cloneNode(true) → deep (node + descendants)
Copies attributes (incl. inline onclick="...")
Does NOT copy addEventListener / node.onclick = fn
Clone has no parent until you appendChild / insertBefore
Same document + same id → fix the clone's id
Clone, optionally fix id, then insert the copy into the tree.
Select the node to copy.
Call cloneNode(true) (deep) or cloneNode() (shallow).
Change duplicate id / name if needed.
Insert with appendChild (or similar).
Piece
Required?
Role
deep
No
true = subtree; default false
Return
—
New Node (not in the document yet)
Listeners
—
Inline attrs yes; addEventListener no
Pseudocode
Pseudocode
original = getElementById("card")
copy = original.cloneNode(true) // deep
if copy.id:
copy.id = "card-2" // avoid duplicate ids
parent.appendChild(copy)
→ original stays; copy is a new sibling/subtree
Reference
Cheat Sheet
Keep these facts handy when copying DOM nodes.
Topic
Detail
Syntax
node.cloneNode() / node.cloneNode(deep)
Default deep
false (shallow)
Returns
Detached clone (no parent yet)
Attributes
Copied (including inline handlers)
JS listeners
Not copied (addEventListener / onclick =)
IDs
May duplicate — rename the clone
vs importNode
Other document / template custom elements
Standard
Baseline Widely available (since July 2015)
Try it
Live Preview
Clone the sample card shallow or deep, then append the copy to the list.
Source
Card title · child text
Browser demo of Node.cloneNode() — deep vs shallow.
Result
Choose deep or shallow, then Clone…
Code
JavaScript Examples
Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.
Example 1 — Deep Clone
MDN-style: copy the node and its subtree.
JavaScript
const p = document.getElementById("para1");
const p2 = p.cloneNode(true);
console.log(p2.textContent);
You must appendChild / insertBefore the clone yourself.
Compatibility
Browser Support
Node.cloneNode() is marked Baseline Widely available on MDN (since July 2015).
✓ Baseline · Widely available
Node.cloneNode()
Returns a duplicate node. deep=true copies the subtree. Attributes copy; addEventListener does not. Watch for duplicate IDs.
100%Widely available
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
cloneNode()Excellent
Bottom line: Safe everyday DOM API. Rename ids, re-bind listeners, and use importNode for other documents / templates. See the MDN cloneNode reference.
Analysis
Performance Notes
Approach
Cost
Best for
cloneNode(false)
Cheap
Empty shell / void elements
cloneNode(true)
O(subtree size)
Full UI card / list item
Many deep clones
Can be heavy
Batch carefully / reuse templates
Deep clones walk the whole subtree. For repeated UI, clone a small template node rather than a huge branch every time.
Remember
Key Takeaways
Deep vs shallow:true copies descendants; default is shallow.
Detached: append the clone yourself.
IDs: rename clones in the same document.
Listeners: re-bind addEventListener on the clone.
One line:cloneNode copies a node (optionally its tree) — insert it, fix ids, re-bind JS listeners.
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 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.