reformed
reformed

Reputation: 4790

How to create a copy of a form with its DOM elements

How does one create a copy of a form with all of its form elements so that the copy can be manipulated and the original is left unchanged?

Upvotes: 3

Views: 2781

Answers (2)

woofmeow
woofmeow

Reputation: 2408

Using plain javascript cloneNode like this

var dupNode = node.cloneNode(deep);

Example

var p = document.getElementById("para1"),
var p_prime = p.cloneNode(deep); 
//If "deep" is set to true it will clone all the child nodes too,
//If set to false then only the node and not the children

Here is the documentation.

Hope that helps.

Upvotes: 7

Smurfette
Smurfette

Reputation: 2005

Use the jQuery clone object as such:

var cloned_object = $( ".hello" ).clone().

and to add it to the dom

cloned_object.appendTo( ".goodbye" );

Here is the reference:

http://api.jquery.com/clone/

Upvotes: 3

Related Questions