plainJS

Functions / Manipulation

Clone an element

jQuery $.clone()

2015 page

This is the original page from 2015, kept at its address while the tested rewrite is in progress. The code is of its time: it ran in Internet Explorer and does not use what browsers have gained since. The rewrite replaces it here.

In short

Create a deep copy of a DOM element.

In modern browsers, including IE 8, there’s a native clone method for replacing jQuery’s $.clone() method:

js
var el = document.querySelector('div');

var foo = el.cloneNode(true);

cloneNode(true) creates a deep copy of the selected DOM element including attributes and child elements. To clone only the node and its attributes, pass false as argument to cloneNode().