1

I have the following event listener on my inputs on a HTML page:

$('input').on('input', function (e) {
    console.log(this.value);
});

I understand the input event captures the following individual events change, keyup and paste.

When I update an input value using jQuery, I want to trigger this event so my event code runs. If I run:

//Does not work
$('input').val('test').trigger('change');

My event does not fire. I expected the input event handler to catch the change event. If I run:

//Does work
$('input').val('test').trigger('input');

This does work... why does triggering the change event not fire my input event handler?

keldar
  • 6,152
  • 10
  • 52
  • 82
  • 1
    No, `oninput` event does not responds to `onchange` event. They are different. It would be really confusing world where one events trigger other events. – dfsq May 08 '15 at 12:13
  • http://jsfiddle.net/arunpjohny/e0xr8u0x/1/ - input event does fire – Arun P Johny May 08 '15 at 12:14
  • 1
    check [this](http://stackoverflow.com/questions/17384218/jquery-input-event) may be helpful to you – Bhushan Kawadkar May 08 '15 at 12:14
  • "why does triggering the change event not fire my input event handler?" Why the hell would one type of event fire another type of event? – Maurice Perry May 08 '15 at 12:26
  • @MauricePerry - reread my question - the input event is fired most definitely when `paste` and `keyup` events are triggered. – keldar May 08 '15 at 12:27
  • 1
    @keidar it does not. If you paste something in the field, or if you type a key that will alter the text, and that will trigger an input event. – Maurice Perry May 08 '15 at 12:36
  • Oh OK - I was under the impression it does. I will go back to the drawing board and do a bit more testing - thanks. – keldar May 08 '15 at 12:43

1 Answers1

3

I expected the input event handler to catch the change event.

That's where you went wrong - the input event handler does not catch change events.

Evan Knowles
  • 7,426
  • 2
  • 37
  • 71