EDIT: after writing this answer, I found out about the HTML5 oninput event, which is much more suitable than key events, because it will define all input forms, including insert, drag and drop, etc. The event is supported in all major browsers except IE 8 and lower, but you can simulate the event in IE by matching it with onpropertychange . Example:
if ("onpropertychange" in myInput && !("oninput" in myInput)) { myInput.onpropertychange = function () { if (event.propertyName == "value") myHandler.call(this, event); } } else myInput.oninput = myHandler;
Please note that onpropertychange does not start when entering informal elements with the contentEditable property contentEditable . See my blog for more information.
<h / "> onkeyup will only work after the key is raised, so use onkeypress and / or onkeydown for best results. If you use a timer with a delay of 0 ms, you can get a new value immediately after updating it:
myInput.onkeydown = function () { var self = this; window.setTimeout(function () { alert(self.value); },0); }
Example: http://jsfiddle.net/fgcYD/
Note that this will not insert or drag text into the field. For those who need other events.
source share