I am trying to distinguish the different use of the keydown, keypress, keyup, input, change event in JavaScript.
If it is a JavaScript autocomplete search box, is it true that we have to use the input event handler?
The reason is:
the
changeevent handler won't be invoked until the user press Enter or leave that input box (by the Tab key or clicking outside of the input box), so thechangeevent cannot possibly fit the purpose of making suggestion when the user types in one more character to the input box.The
keydownevent handler can be used to "add" the keystroke to the search term, but for CTRL-v or CMD-v (on Mac) to paste it, we can't really get thekeyCodeone by one if we paste a word such ashellointo the search box -- because only onekeydownwill be for the CTRL and onekeydownfor the v, instead ofhello-- but we can use the input box'svalueattribute to get the value -- however, what if the user uses the mouse to right click and choose "paste" to add text to the box -- in which case should we, or can we use a mouse event handler to look at thevalueattribute? It is just too messy to deal with such low level of keyboard and mouse.
So the input event handler seems to just fit the exact purpose because ANY value change, the input event handler will be invoked. And that's why the input event handler can be important and useful.
We still need the keydown event handler, because what if the user presses the Down Arrow key to go down on the list of possible item? (and possibly the ESC to make the autocomplete suggestion box disappear). In these cases, the input event handler and the change event handler won't be invoked, and the keydown event will be useful for these cases.
Is the above concept correct, mainly for understanding the input event?
(A jsfiddle for understanding what events handlers are called: http://jsfiddle.net/jYsjs/ )