-
-
Save zacwasielewski/20e9c317a9d48cba5fa8 to your computer and use it in GitHub Desktop.
Copy form data with jQuery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form id="form1"> | |
<input name="name" value="Zac"> | |
<input name="address" value="407 French Road"> | |
<select name="gender"> | |
<option></option> | |
<option selected>Male</option> | |
<option>Female</option> | |
</select> | |
</form> | |
<button id="copy_button">Copy →</button> | |
<form id="form2"> | |
<input name="name" value=""> | |
<input name="address" value=""> | |
<select name="gender"> | |
<option></option> | |
<option>Male</option> | |
<option>Female</option> | |
</select> | |
</form> | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('#copy_button').click(function(){ | |
copyFormValues( $('#form1'), $('#form2') ); | |
}); | |
function copyFormValues ( $source_form, $target_form ) { | |
$source_form.find(':input').each(function(){ | |
var field_name = $(this).attr('name'), | |
field_value = $(this).val(); | |
$target_form.find('[name='+field_name+']').val(field_value); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment