Reputation: 49384
I am using:
$('#input1').change(function() {
$('#input2').val($(this).val());
});
The above works fine when you add or change something.
My question is, what do I use when I want to get the value of input1 and assign it to input2 eventhough input1 has not been changed?
Upvotes: 0
Views: 34
Reputation: 28513
you can use below script to add value on page load :
$(document).ready(function(){
$("#input2").val( $("#input1").val() );
});
Upvotes: 1
Reputation: 672
You have to trigger it somehow.
For example with a button.
$("myButtonSelector").click(function(){
$("#input2").val( $("#input1").val() );
});
or on focus
$("#input1").focus(function(){
$("#input2").val( $(this).val() );
});
Or on any other event you would need.
Upvotes: 1
Reputation: 9637
use trigger to change
$('#input1').change(function() {
$('#input2').val($(this).val());
}).trigger("change");
Upvotes: 1