I need an auto-complete in the app I'm working on and, since I'm already using jQuery UI, I'm trying to make its Autocomplete widget meet my needs.
Step one is to make the search term only match at the beginning of suggested terms. I've already got that working, as you can see in the code below. Step two is to have the first suggestion actually autocomplete.
Which probably requires a bit of explanation. When I hear "autocomplete", I envision typing "f" and having the contents of the text field change to "foo", with the "oo" selected, so that it is replaced if I type another character and left in the field if I tab out of it. I'd normally call what the Autocomplete widget does suggesting, rather than autocompleting.
Looking at how Autocomplete works internally, I think the autocompleteopen
event is the correct place to do this (it's called every time the list of suggestions is updated), but I'm at a loss as to how to access the suggestion list from there.
Any thoughts?
$("#field").autocomplete({
delay: 0,
source: function filter_realms(request, response) {
var term = request.term.toLowerCase();
var length = term.length;
response($.grep(candidates, function(candidate) {
return candidate.substring(0, length).toLowerCase() === term;
}));
},
open: function(event, ui) {
// magic happens here?
}
});
For this, we are going to use the jQuery inbuilt function called autocomplete. We will do that in two different stages. Create a basic HTML file and add an input bar to it. Implement the autocomplete functionality.
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.
A user types into an input, and the autocomplete “completes” their thought by providing full terms or results: this is the very base of an autocomplete experience. For example, try typing the letter “m” in the search box below. You can select suggestions like “macbook” and “mobile”.
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.
Got it. I'd forgotten that widgets can be accessed via .data()
.
$("#field").autocomplete({
delay: 0,
source: function filter_realms(request, response) {
var term = request.term.toLowerCase();
var length = term.length;
response($.grep(candidates, function(candidate) {
return candidate.substring(0, length).toLowerCase() === term;
}));
},
open: function(event, ui) {
var current = this.value;
var suggested = $(this).data("autocomplete").menu.element.find("li:first-child a").text();
this.value = suggested;
this.setSelectionRange(current.length, suggested.length);
}
});
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