English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

HTML DOM cloneNode() Method

HTML DOM Element Object

cloneNode()The function of the method is: return a copy of the node that calls this method.

cloneNode() method clones all properties and their values.

UseappendChild()orinsertBefore()The method inserts the cloned node into the document.

Note: cloneNode() may cause duplicate element IDs in the document.

Syntax:

node.cloneNode(deep)
var node = document.querySelector("#box").firstElementChild;
var copy = node.cloneNode(true);
document.getElementById("result").appendChild(copy);
Test and See‹/›

Browser Compatibility

All browsers fully support the cloneNode() method:

Method
cloneNode()YesYesYesYesYes

Parameter Value

ParameterDescription
deep(Optional) Specify whether all descendants of the node should be cloned
Possible Values:
  • True -The cloned node, its attributes, and its descendants

  • False -Only clone the node and its attributes (default).

Technical Details

Return Value:A Node object representing the cloned node
DOM Version:DOM Level1

More Examples

Copy a DIV element (including all its attributes and child elements) and append it to the document:

var node = document.querySelector("#box");
var copy = node.cloneNode(true);
document.body.appendChild(copy);
Test and See‹/›

Related References

HTML DOM Reference:document.adoptNode() method

HTML DOM Reference:document.importNode() method

HTML DOM Element Object