Reputation: 2131
I'd like to show autocomplete suggestions only after the user presses the Enter key.
After reading the api docs, I suspect I need to overwrite the search
event or the change
one, however I am unclear how to. In particular, the search
event is:
Triggered before a search is performed, after minLength and delay are met. If canceled, then no request will be started and no items suggested.
What I'd like to do is cancel the event unless enter is pressed. How do I do this in the init function below?
$(".selector").on("autocompletechange", function(event, ui) {});
Or is there another better way to do this?
Upvotes: 1
Views: 731
Reputation: 738
You need to alter your jquery lib events in order to catch the event of enter key and perform your functionality.In this function only enter key will open dropdown and the rest events will be disabled.
$(".selector").autocomplete({
keypress: function (event, ui) {
if (event.which == 13) {
console.log("Key Pressed");
}
else
return false;
}
});
Upvotes: 1
Reputation: 2710
My suggestion would be let the onchange event be called every time, but you specifically fetch the details when Enter
key is pressed. You can detect the key pressed from the event. Below is a sample -
function(event, ui) {
if(event.keyCode == 13){
//write code to populate the Autocomplete
}
}
Upvotes: 0