0

I have 2 input that i send some value from input1 to input2 i want detect when input2 is changed.

HTML

<input type="text" id="input1"/>
<input type="text" readonly id="input2"/>

JS

$('#input1').on("change" , function() {
  if($("#input1").val() == "550") {
     $("#input2").val("2012")
  }
  else if($("#input1").val() == "650") {
     $("#input2").val("2013")
  }
  else {
     $("#input2").val("")
  }
})

now i want detect when input2 have value and when is empty

this code don't work because it when happened that input have changed with keyboard or etc

$('#input2').on('input',function(e){
     alert('Changed!')
});

I need a code like that to detect every changes in input2 , thanks

2 Answers 2

3

You can try this:

$('#input1')
    .on('keyup input', function(){
        var value = $(this).val();
        var $input2 = $('#input2');
        switch(value) {
            case "550":
                $input2
                    .val("2012");
                $input2
                    .trigger('change');
                break;
            case "650":
                $input2
                    .val("2013");
                $input2
                    .trigger('change');
                break;
            default:
                $input2
                    .val("");
        }
    });
$('#input2')
    .on('change', function(){
        alert($(this).val());
    });

Here is the FIDDLE.

Sign up to request clarification or add additional context in comments.

1 Comment

wonderful, thank you so much @Beginner, .trigger('change'); is my solution
0

Try this:put your entire code inside input1 input event.If you want to copmare with old input2 value then check it before assigning new value to it.

$('#input1').on('blur',function(e){
   var values="";
     if($(this).val() == 550) {
          values="2012";
}
else if($(this).val() == 650) {
   values="2013";
}
else {
   values="";
}

if($("#input2").val() == values){
alert("no change");
}else{
$("#input2").val(values);
alert("changed");
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="input1"/>
<input type="text" readonly id="input2"/>

1 Comment

Thank you,I have not problem for show 2012 or 2013 in input2, i have problem to detect when input2 is changing from 2012 to 2013 or going to empty value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.