Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if ENTER was used to select a jQueryUI autocomplete suggestion or not?

Using jQuery 1.7.2 and jQueryUI 1.8.12, I'm trying to implement an autocomplete field with the following behavior:

  • If an autocomplete suggestion is selected (with the mouse or keyboard), call function A
  • If there are no matches (hence no suggestion was selected) and ENTER was pressed, call function B

The problem I'm having is, when I select a suggestion with the keyboard, my check for ENTER is also fired, and that would make me call function B when I shouldn't. Here is a simple example (also on jsfiddle):

$('input').autocomplete({
    source: ['foo', 'bar', 'baz' ],
    select: function(event, ui) {
      console.log('suggestion selected');   
    }
}).keydown(function(e) {
    if(e.keyCode == 13) console.log('ENTER (keydown)');
}).keyup(function(e) {
    if(e.keyCode == 13) console.log('ENTER (keyup)');
});

According to the console, the keyup/keydown handlers are firing after the autocomplete select event (which is weird), so I tried to prevent it (by either trying to top bubbling, preventing the default, or both). I also traversed the event object's originalEvent properties up to the topmost parent (the original KeyboardEvent), but that seems unstoppable either.

Does anyone know a simple solution to what I'm trying to accomplish, preferably avoiding flags to indicate that the select event was fired? I'd like to avoid that, as such a flag would have to be cleared somewhere else, which seems error-prone.

like image 487
bfavaretto Avatar asked Apr 19 '13 19:04

bfavaretto


People also ask

What is AutoComplete select?

The Select Autocomplete component is a mix between a <select> element and a text input. The focus on the input triggers a selection dropdown; by typing in the input element, you'll filter the select options. This component uses the <select> element to create (in JS) the dropdown.

What is AutoComplete dropdown?

Enables users to quickly find and select from a pre-populated list of values as they type, leveraging searching and filtering.

What is AutoComplete control?

The AutoComplete control is an extender control that provides AutoCompletion services to any edit control on the same form as the AutoComplete control. AutoCompletion can be defined as prompting you with probable matches during data entry.

What is jQuery AutoComplete?

In the process of searching a specific value, the jQuery UI autocomplete selection feature provides the user with some string suggestions for the input area which effectively saves time. The autocomplete select action is triggered when the user selects one of the options from the pre-populated list.


2 Answers

How about this. Before keydown event registry, just bind an autocomplete.keydown event. This is guaranteed to execute before your keydown. Note the order of event registration also matter.

http://jsfiddle.net/zFPZH/

$('input')
    .autocomplete({
        source: ['foo', 'bar', 'baz' ],
        select: function(event, ui) {
            console.log('suggestion selected');         
        }
    }).bind("keydown.autocomplete", function() {
        if(event.keyCode == 13){ 
            console.log('keydown.autocomplete (keyup)'); 
        };
    }).keydown(function(e) {
        if(e.keyCode == 13){ 
            console.log('ENTER (keydown)');
        };
    }).keyup(function(e) {
        if(e.keyCode == 13){ 
            console.log('ENTER (keyup)');
        };
    });
like image 175
PSL Avatar answered Oct 02 '22 18:10

PSL


It seems that when the suggestion list has appeared and a suggestion is highlighted, it has the class ui-state-hover. When checking the keyCode in your keydown event, you could also check for the presence of an element with that class in the suggestion menu to see if, when enter is pressed, it is to confirm the suggestion.

like image 28
Ashley Strout Avatar answered Oct 02 '22 16:10

Ashley Strout