0

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());
  }
});
5
  • Why not trigger the change event when your code changes the value of #b inside the success of your AJAX call ? $('#b').trigger('change'); Commented Feb 5, 2018 at 10:05
  • You are meaning in/with the success procedure? Nope, because I've too update a lot of other fields Commented Feb 5, 2018 at 10:08
  • 1
    Select them all in your JQuery selector, then trigger the change event : $('#b,#c,...').trigger('change'); or $('#commonParent input').trigger('change'); Commented Feb 5, 2018 at 10:10
  • Sorry, I can't follow you. By trigger the change event in your way, this isn't working well. Sure he is jumping into the on('change.... event listener, but this is not solving my issue to jump into this listener after the value of #b has changed by the ajaxcall. or am I wrong? Commented Feb 5, 2018 at 10:18
  • Trigger them inside your AJAX call success ! Commented Feb 5, 2018 at 10:20

1 Answer 1

1

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

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

2 Comments

hmmm I guess I got you. To use a intervall is bad from my point of view, because it is fired again and again and cost a lot of RAM. The best way is to fire my own privat event after the success ajax. thats it. thx
@user1357971 yes I agree with you, but also you can start it and end it when change is triggered.

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.