Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

autocomplete results array

I have set up an autocomplete feature for an input box, with jQuery UI's .autocomplete().

How can I access the array (or object?) of results that drop-down when I start typing?

I would like to use the results to highlight various other elements on my page.

like image 690
Randomblue Avatar asked Dec 06 '25 01:12

Randomblue


2 Answers

There are two ways you can accomplish this.

  1. Use the source option to define logic for filtering results. This logic would be identical to the widget's source code:

    $("#auto").autocomplete({
        source: function (request, response) {
            var results = $.ui.autocomplete.filter(source, request.term);
    
            $("#results").text(results.join(", "));
    
            response(results);
        }
    });
    

    Example: http://jsfiddle.net/andrewwhitaker/27S6p/

    If you're using a remote datasource, this is easy to integrate into your AJAX response.

  2. If you needed a solution for multiple autocomplete widgets, you could override the _response function to raise a special event that you can bind to:

    var __response = $.ui.autocomplete.prototype._response;
    $.ui.autocomplete.prototype._response = function(content) {
        __response.apply(this, [content]);
        this.element.trigger("autocompletesearchcomplete", [content]);
    };
    
    $("#auto").autocomplete({
        source: src
    }).bind("autocompletesearchcomplete", function (event, results) {
        $("#results").text(results.join(", "));
    });
    

    Example: http://jsfiddle.net/andrewwhitaker/V9Vun/

    I would only use this solution if you have several widgets on the page and you need to do this for all of them.

Neither of them are ideal but should get the results you're looking for.

like image 166
Andrew Whitaker Avatar answered Dec 07 '25 17:12

Andrew Whitaker


Take a look at result event. I think it will give you the array of objects which it shows in the auto complete.

http://docs.jquery.com/Plugins/Autocomplete/result#handler

like image 20
ShankarSangoli Avatar answered Dec 07 '25 17:12

ShankarSangoli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!