140
rated 0 times
[
145]
[
5]
/ answers: 1 / hits: 21072
/ 10 Years ago, tue, february 25, 2014, 12:00:00
What would be the best way to copy input values from one form to another where the inputs in each form have the same name? I came up with the following, however, it seems terribly inefficient (I know, efficiency probably doesn't matter, but would still like to know). Thanks
<!DOCTYPE html>
<html>
<head>
<meta http-equiv=Content-Type content=text/html; charset=utf-8>
<title>Testing</title>
<script src=https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js type=text/javascript></script>
<script type=text/javascript>
$(function(){
$('#copy').click(function(){
var form1=$('#form1').find(':input');
var form2=$('#form2');
form1.each(function() {
var $t=$(this);
form2.find('[name='+$t.attr('name')+']').val($t.val());
});
});
});
</script>
</head>
<body>
<button id=copy>copy</button>
<form id=form1>
<input name=a type=text>
<input name=b type=text>
<input name=c type=text>
<input name=d type=text>
</form>
<form id=form2>
<input name=a type=text>
<input name=b type=text>
<input name=c type=text>
<input name=d type=text>
</form>
</body>
</html>
More From » jquery