Andrew
Andrew

Reputation: 239107

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 element is only fired when two conditions are true:

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?

Upvotes: 0

Views: 475

Answers (2)

bobince
bobince

Reputation: 536735

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.

Upvotes: 2

Dan Davies Brackett
Dan Davies Brackett

Reputation: 10081

you have access to the keypress, keyup, and keydown events; the details of when those fire can be found at Quirksmode.

Upvotes: 2

Related Questions