Clone an element

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:

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().