Clone node structure
Quick answer
cloneNode(true) copies the subtree; cloneNode(false) copies the node without its children. Listeners registered through addEventListener are not copied. Check IDs before inserting a clone.
Browser example
Run in a browser document, not a bare Node.js process.
const a=document.createElement("div");a.append(document.createElement("span"));const b=a.cloneNode(true),c=a.cloneNode(false);console.log(b.children.length,c.children.length,b===a);Expected console output:
1 0 falseCommon mistake
A deep DOM clone is not a copy of arbitrary JavaScript state.
Practice
Clone a list and change a child in the copy.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Clone node structure — Knowledge Check
Check the central concept, apply it to a new scenario, and predict browser behavior.
Which argument includes descendant nodes?
What does cloneNode(false) preserve from the original subtree?
What must be considered before inserting a cloned element with an ID?
What happens to listeners registered with addEventListener when the element is cloned?
What is printed by this browser snippet? (Clone node structure)
const p=document.createElement("div");p.append(document.createElement("span"));const c=p.cloneNode(true);c.firstElementChild.textContent="copy";console.log(p.firstElementChild.textContent==="",c.firstElementChild.textContent);