Satch3000
Satch3000

Reputation: 49384

JQuery Onchange when not changed

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

Answers (3)

Bhushan Kawadkar
Bhushan Kawadkar

Reputation: 28513

you can use below script to add value on page load :

$(document).ready(function(){
 $("#input2").val( $("#input1").val() );
});

Upvotes: 1

wick3d
wick3d

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

Balachandran
Balachandran

Reputation: 9637

use trigger to change

$('#input1').change(function() { 

          $('#input2').val($(this).val());

 }).trigger("change");

Upvotes: 1

Related Questions