Using jQuery 1.7.2 and jQueryUI 1.8.12, I'm trying to implement an autocomplete field with the following behavior:
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.
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.
Enables users to quickly find and select from a pre-populated list of values as they type, leveraging searching and filtering.
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.
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.
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)');
};
});
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With