Web APIs

Node.cloneNode()

Try the free JavaScript compiler

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.

Formal Syntax & Structure

javascript syntaxjavascript
node.cloneNode([deep])

Parameters & Attributes

NameTypeDescription
deepbooleanInclude descendants when true.

Return Value

Node

Cloned node.

Working Example

Example Snippetjavascript
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);
JavaScript Reference