views:

241

answers:

2

I'm using jquery autocomplete and want it to focus a specific control once an item is selected from the autocomplete list. I tried $('#nextelement').focus(), and it receives focus, but the focus goes back to the autocomplete control immediately. How can I retain focus to the next control when autocomplete closes?

[edit] I'm using autocomplete plugin that comes with Yii CAutoComplete widget, which the documentation states is this one: http://plugins.jquery.com/project/autocompletex

A: 

Have you tried:

setTimeout(function() {
  $('#nextelement').focus();
}, 1);

to change the focus after the current event loop is done?

Pointy
That does work, but maybe there is a more beautiful solution :)
Rytis
Which autocomplete plugin are you using? There are several of them. It might be possible to explicitly *tell* the plugin that you don't want it to restore focus to the input field, but how to do that would depend on which plugin you're using.
Pointy
I don't see any way to override the behavior of the plugin via the "result" handler.
Pointy
A: 

This to me smells of a logic/use case of exactly when and what.

IF you select a value autocomplete that is valid you can check the change state of the control in focus of the autocomplete.

What if you do select one and how? (clicked, hit enter, just changed etc.)

I would think using the .change() event on the focused element would be best, then focus on the next element in your form - which depends on your construction of your page - sibling, next input, next by id, next sibling in the class, what to focus of this is the last element etc.

Mark Schultheiss
Well the problem is likely to be that directly adding event handlers will probably collide with the event handling being done by the plugin itself. To me, when you're using some fancy plugin like that, it's better to add handlers through the API of the plugin itself. This plugin has a "result" handler I think.
Pointy
Yes, but what this means to me is: react to the change event to focus somewhere else, thus it is outside (after) the autocomplete has done its business.
Mark Schultheiss