Skip to content

Instantly share code, notes, and snippets.

@rafaellyra
Created December 13, 2012 02:55
Show Gist options
  • Save rafaellyra/4273649 to your computer and use it in GitHub Desktop.
Save rafaellyra/4273649 to your computer and use it in GitHub Desktop.
How to clone a dom element using native javascript I needed to clone a dom element in a project and have searched this at google and stackoverflow but i just finded a lot of big functions to do this, looking at jsref I findend the cloneNode method, a native javascript method to clone dom elements.
//Syntax
var dupNode = node.cloneNode(deep);
/*
node -> The node to be cloned.
dupNode -> The new node that will be a clone of node
deep (Optional) ->
[true] if the children of the node should also be cloned, or [
false] to clone only the specified node.
*/
//Example
var p = document.getElementById("para1"),
p_prime = p.cloneNode(true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment