Brian Ogden
Brian Ogden

Reputation: 19212

Auto Select Single Match jQuery UI Autocomplete Combobox

I am using a slightly popular Combobox UI element which is a custom jQuery UI Autocomplete extension: see here

I would like, if there is a single match to select that match and also fire the selected event, specifically changing this section of code in the Autocomplete Combobox should be the solution:

    _source: function (request, response) {
                var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
                response(this.element.children("option").map(function () {
                    var text = $(this).text();
                    var value = $(this).val();
                    if (this.value && (!request.term || matcher.test(text)))
                        return {
                            label: text,
                            value: value,
                            option: this
                        };
                }));
            }

I am not familiar with the jQuery map function, its syntax does not feel familiar to me even though I am very familiar with jQuery and vanilla Javascript, though I understand what the function is doing.

I would like to something like:

if(match.count == 1)
   //fire select event and/or select option in hidden select list

Upvotes: 3

Views: 2336

Answers (1)

Balamurugan
Balamurugan

Reputation: 116

var data = [
  "Apple",
  "Orange",
  "Pineapple",
  "Strawberry",
  "Mango"
];

$(document).ready(function () {
    $( "#fruits" ).autocomplete({
        source: data,
        autoFocus: true,
    });
});

Upvotes: 2

Related Questions