Skip to content

Instantly share code, notes, and snippets.

@hcosta
Last active December 27, 2015 12:19
Show Gist options
  • Save hcosta/7324561 to your computer and use it in GitHub Desktop.
Save hcosta/7324561 to your computer and use it in GitHub Desktop.
Clone all html elements in a div with all form values dinamically
/* Requires jQuery*/
function clone(from, to){
// Do this before clone
$('[type=text], textarea').each(function(){ this.defaultValue = this.value; });
$('[type=checkbox], [type=radio]').each(function(){ this.defaultChecked = this.checked; });
$('select option').each(function(){ this.defaultSelected = this.selected; });
// Then clone
var html_content = $(from).html();
$(to).html(html_content);
}
/* Usage */
clone("div#source", "div#target");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment