views:

45

answers:

3

What is the correct event to use with autocomplete-type functionality? I'm doing something similar to autocomplete and tried using the "keypress" event, however, when I go to get the text out of the input that fired the even, it only has the text before the event was fired. Should I be using a different event, or perhaps just append the new character onto the existing text?

+2  A: 

You want to use the keyup event to have the current value of the input.

Not the jQuery UI autocomplete does not do this, but for a different reason. It has a small delay between when they key is pressed and when a search occurs, so the value is populated by then. It's actually using the keydown event for faster response to arrow keys and such.

Nick Craver
+2  A: 

At keypress, nothing has been put in. Use the keyup event, and you'll have the value after the key was pressed.

EMMERICH
A: 

better set an interval/timeout function on focus (remove it on blur) to:

  1. Check the number of char entered and change of content
  2. Perform the autocomplete query
  3. perfom auto completion
dvhh
Why remove the function on blur?
DutrowLLC
@DutrowLLC to avoid an additional "background" timeout/interval function that is doing nothing but eating "little" cpu time. this is also more convenient for debugging if you only run what you think is necessary.
dvhh
You mean a function that loops continuously?
DutrowLLC
@DutrowLLC the nterval would of course loop itself, and the setimeout one would reset another timeout of the same function to be called again.I think you would better of with something that was already created. setinterval and settimeout can somtime cause some headache.
dvhh
You'd probably be better off just responding to events
DutrowLLC
That really depend on the amount of code you are putting in your event.
dvhh