jquery on handler to fire when setting element value through jquery

This question already has an answer here:

  • How to fire JQuery change event when input value changed programmatically?

    5 answers

I have an event handler that fires whenever a user clicks into a box and enters text:

$( '#FOO' ).on( 'keyup change paste', function () {
   console.log('this event handler has fired.');    

I have another piece of code where, instead of allowing the user to type something in themselves, I am setting the value of the element myself:


While this code works, setting the text value of the element named FOO to a value of BAR, it does not trigger the event handler. Which on event am I missing, apart from keyup change paste that I’m already using?

Those events are only registered when the user initiates them. Since you are making the change yourself, you have to simulate the event using trigger().