I’ve got a “search” box that is supposed to be able to search data “live”. Right now if I attach the “keypress” event to it and update the results, it works pretty good. However, if they press backspace it doesn’t account for that (refresh the results).
I understand I can probably account for the “backspace” key.
But am I missing any other possibilities? I want any change of the text in the input to be triggering the “event” or calling the refresh function.
The one thing I don’t want to do is set “alarms” or “timers” to check every so often if it has changed.
Ideas?
To insure that you are triggering your data search after each changing of the text field, you should check if the text field has changed after each
.keyup()or.keydown()Try it out with this jsFiddle
To handle mouse copy paste (which Filipe pointed out doesn’t work with the above), jQuery has the option of binding multiple events to one element, and it has a
pasteandcutevent. The problem with these is that they are triggered immediately on paste but BEFORE the input box contents are actually changed, so we need a timeout… in fact a timeout would be a nice feature for the whole thing, so that if the user is typing quickly, then we wait until they are done:So now we check for the keyboard, for pasting in, and for cutting out.
Try it out with this jsFiddle