In my experience, input type="text"
onchange
event usually occurs only after you leave (blur
) the control.
Is there a way to force browser to trigger onchange
every time textfield
content changes? If not, what is the most elegant way to track this “manually”?
Using onkey*
events is not reliable, since you can right-click the field and choose Paste, and this will change the field without any keyboard input.
Is setTimeout
the only way?.. Ugly :-)
You could use the
keydown
,keyup
andkeypress
events as well.Please, judge next approach using JQuery:
HTML:
Javascript(JQuery):
2018 here, this is what I do:
If you can point out flaws in this approach, I would be grateful.
"input" worked for me.
Update:
See Another answer (2015).
Original 2009 Answer:
So, you want the
onchange
event to fire on keydown, blur, and paste? That's magic.If you want to track changes as they type, use
"onkeydown"
. If you need to trap paste operations with the mouse, use"onpaste"
(IE, FF3) and"oninput"
(FF, Opera, Chrome, Safari1).1Broken for
<textarea>
on Safari. UsetextInput
insteadTo track each try this example and before that completely reduce cursor blink rate to zero.
onblur : event generates on exit
onchange : event generates on exit if any changes made in inputtext
onkeydown: event generates on any key press (for key holding long times also)
onkeyup : event generates on any key release
onkeypress: same as onkeydown (or onkeyup) but won't react for ctrl,backsace,alt other