user1357971
user1357971

Reputation: 63

Trigger change of input value without user interaction in the browser

I've two input fields, let's say #a and #b.

By clicking #a and inserting a char I fire an AJAX request. The response is filling the value of #b.

How can I detect any changes here?

The code above only works by doing directly something in the browser

$('#b').on('change input keyup', function(){
  var that = $(this);
  console.log('changed');
  if(that.val()){
    console.log(that.val());
  }
});

Upvotes: 0

Views: 878

Answers (1)

Amr Elgarhy
Amr Elgarhy

Reputation: 68912

As listed on jquery docs change event:

Note: Changing the value of an input element using JavaScript, using .val() for example, won't fire the event.

So you will need to trigger your change event manually after your ajax success or convert the event to a function and just call it after ajax success.

Some developers went with setinterval way, but I think this is needed for special cases but just wanted to mention other options

Upvotes: 1

Related Questions