I'm working a web page where I'd like to run some Javascript code when a user alters text in a given input field, but I can't figure out which event to trap (assuming one exists) that would give me the behavior I'm looking for. onFocus
happens too soon -- if the user selects the field but doesn't change any text, I don't want anything to happen. But onChange
is too late -- I'd like the Javascript to fire as soon as the user starts typing, not when the user is done typing and clicks something else. How could I accomplish this?
views:
206answers:
3
+5
A:
onkeydown
(although it wouldn't work for pasted data, where onchange
is probably better).
David Dorward
2009-11-17 15:57:47
Bingo! Exactly what I was looking for, thanks.
BlairHippo
2009-11-17 15:59:57
... and extra thanks for the "onChange" caveat, I don't know that I would have caught that.
BlairHippo
2009-11-17 16:08:01
Thanks, but David beat you to it. +1 for the effort, though, it is appreciated.
BlairHippo
2009-11-17 16:04:06
+4
A:
There are a couple events that can help you:
- onKeydown (fired when a key is pressed down)
- onKeyup (fired when a key is released)
- onKeypress (fired when a key is pressed and then released)
Alex
2009-11-17 15:58:49
looks like i got beat to it... thats what I get wasting time formatting my answer :P
Alex
2009-11-17 15:59:33
Yeah, the site does reward knowledge AND fast typing, doesn't it? +1 for the effort, though, it is appreciated.
BlairHippo
2009-11-17 16:02:55
I consider it good form to wait at least an hour before accepting an answer.
Dan Hook
2009-11-17 16:05:08