I have <input>
elements that use autocomplete and they also have initial values.
The problem is that my users expect to click on the input and use the up/down arrow keys to change those values -- just like autocomplete allows if there is no initial value (and also just like a <select>
behaves).
HTML:
<label>
Input with 4 possible values:
<input type="text" value="green" id="tstInp">
</label>
jQuery:
$("#tstInp").autocomplete ( {
delay: 0,
minLength: 0,
source: ["red", "green", "blue", "yellow"]
} )
jsFiddle:
To duplicate the problem:
<select>
. And, you will be able to filter values by typing letters.Ideally in step 3, not only would the arrow keys cycle through the possible values, but they would start at the initially entered value (green
in this example). EG:
The only difference between the expected behavior and what autocomplete currently does is that immediately after the input is focused, the initial up/down arrow keys should show all possible values -- ideally with the initial value already focused in the dropdown menu.
How can I get autocomplete to respond to the up/down arrows as expected?
I'm using jQuery 2.1.0 and jQuery-UI 1.11.1.
Here is one possible solution:
var source = ["red", "green", "blue", "yellow"],
showall;
$("#autocomplete1").autocomplete({
minLength: 0,
search: function(event, ui) {
showall = event.which === 40;
},
source: function(request, response) {
response(showall ? source : $.ui.autocomplete.filter(source, request.term));
}
});
Original answer:
It is possible to open the dropdown and show all values manually (e.g. on click of a button) by (i) setting minLength: 0
(ii) calling search
method and passing an empty string. You can ook this behavior in the focus event of the textbox or add a dedicated button for this purpose.