I want to do something when a keypress changes the input of a textbox. I figure the keypress event would be best for this, but how do I know if it caused a change? I need to filter out things like pressing the arrow keys, or modifiers… I don’t think hardcoding all the values is the best approach.
So how should I do it?
In most browsers, you can use the HTML5
inputevent for text-type<input>elements:This doesn’t work in IE < 9, but there is a workaround: the
propertychangeevent.IE 9 supports both, so in that browser it’s better to prefer the standards-based
inputevent. This conveniently fires first, so we can remove the handler forpropertychangethe first timeinputfires.Putting it all together (jsFiddle):