Element

Element.prototype.cloneNode

Returns a duplicate of the node on which this method was called

Syntax

JavaScript
element.cloneNode(deep?)

Parameters

ParameterTypeDescription
deepbooleanIf true, clone the node and all its descendants. Default is false

Return Value

A new Node that is a clone of this node

Examples

Basic Usage
const original = document.querySelector('.template')!
const clone = original.cloneNode(true) as HTMLElement
clone.id = 'clone-1'
document.body.appendChild(clone)
Practical Example
function duplicateRow(table: HTMLTableElement) {
  const lastRow = table.rows[table.rows.length - 1]
  const clone = lastRow.cloneNode(true) as HTMLTableRowElement
  table.tBodies[0].appendChild(clone)
}
Advanced Usage
const template = document.getElementById('card-template')!
const cards = [1, 2, 3].map(() => template.cloneNode(true) as HTMLElement)
cards.forEach(card => document.body.appendChild(card))

Understanding Element.prototype.cloneNode

The Element.prototype.cloneNode method in JavaScript returns a duplicate of the node on which this method was called. It belongs to the Element object and is one of the most widely used methods for working with element values in modern JavaScript and TypeScript applications.

The method signature is element.cloneNode(deep?). It accepts 1 parameter: deep. When called, it returns a new node that is a clone of this node. Understanding when and how to use cloneNode() helps you write more expressive, readable code.

Common use cases for Element.prototype.cloneNode include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like dom-createelement, dom-appendchild, dom-append, enabling you to chain operations together for complex data manipulation pipelines.

Browser support for Element.prototype.cloneNode is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.

Browser Compatibility

Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.

Related Methods

More Element Methods

Other methods in the Element object

Related Tools

More Element Methods

Explore JavaScript Methods

Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.