0

The code below works great:

this.on('keypress', '[data-' + options.dataClass + ']', function () {
alert(1);
});

This also works for keydown, keyup, ... but the code below doesn't work

this.on('change', '[data-' + options.dataClass + ']', function () {
alert(1);
});

By the way the elements matching selector are input with type of text. what is wrong?

Edit: Demo

Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
  • 4
    From the jQuery website `The change event is sent to an element when its value changes. This event is limited to elements, – EmptyArsenal Nov 22 '13 at 19:55
  • try this on a select, as arsenal stated, it'll work..not so much on a text, you need to harness focus..this.on('change','select',function(){ alert(1); }); – foxtrotZulu Nov 22 '13 at 20:05
  • well it might work great for select but i need it for input – Ashkan Mobayen Khiabani Nov 22 '13 at 20:06
  • try this...this.on('input','[data-' + options.dataClass + ']', function() { alert(1); }) – foxtrotZulu Nov 22 '13 at 20:11

1 Answers1

4

the change event only fires when the input is blurred (and there have been changes made since it gained focus). I think what you are looking for is the input event. This will trigger immediately whenever the input is changed by a keypress or paste.

Be warned that the input event only works on newer browsers.

Documentation for input event

jbabey
  • 45,965
  • 12
  • 71
  • 94
  • well if it is so. and If I want to have old browser support too, what should I do? take my plugin back to work with `keyup`? and is there a way to check if `input` is working in this browser or not? – Ashkan Mobayen Khiabani Nov 22 '13 at 19:59
  • @AshkanMobayenKhiabani the way to replicate the `input` event is to bind to `paste`, `keydown`, `keyup`, `keypress`, and `dragdrop`. It is quite annoying to write. see other questions such as [this](http://stackoverflow.com/questions/6458840/on-input-change-event) – jbabey Nov 22 '13 at 20:04