Javascript: How to trigger an "onchange" event while the text element still has focus?
As far as I understand, the "onchange" event of a text input elemen开发者_开发问答t is only fired when two conditions are true:
- The text has changed.
- The field no longer has focus.
So when you click off, it fires the event. How can I fire an event when the text has changed, but the field still has focus?
you have access to the keypress
, keyup
, and keydown
events; the details of when those fire can be found at Quirksmode.
Reliably? You can't, yet.
You can catch the majority of changes by firing your update checker onkeyup
as well as onchange
. However whilst this picks up simple keyboard interactions, it fails to trigger on other events like cut-and-paste (there is onpaste
in some browsers, but you can't rely on it), drag-and-drop, undo-redo and spellchecker changes.
HTML5 proposes oninput
to solve this, but the implementation isn't there yet. If you really want to spot all input changes today you will have to setInterval
a function that keeps checking the input's value to see if it has changed.
精彩评论