Using jQuery 1.7.2 and jQueryUI 1.8.12, I am trying to implement an autocomplete field with the following behavior:
- If an auto-complete suggestion is selected (using the mouse or keyboard), call function A
- If there are no matches (therefore no suggestions were suggested) and ENTER is pressed, call function B
The problem I am facing is that when I select a sentence using the keyboard, my check for ENTER also starts, and this forces me to call function B when I do not. 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, keyup / keydown handlers are fired after an autocomplete selection event (which is strange), so I tried to prevent it (by trying top spoofing, default prevention, or both). I also traversed the properties of the originalEvent event object to the highest parent (original KeyboardEvent), but this too cannot be stopped.
Does anyone know a simple solution to what I'm trying to execute, preferably avoiding flags to indicate that the select event was fired? I would like to avoid this, since such a flag should be cleared elsewhere, which seems unacceptable.
source share