Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making jQuery UI's Autocomplete widget *actually* autocomplete

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?
    }
});
like image 601
Ben Blank Avatar asked Sep 11 '10 01:09

Ben Blank


People also ask

How can create autocomplete search box in jQuery?

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.

How does jQuery autocomplete work?

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.

What is autocomplete example?

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”.

What is autocomplete control?

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.


1 Answers

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);
    }
});
like image 108
Ben Blank Avatar answered Oct 16 '22 06:10

Ben Blank