Yetimwork Beyene
Yetimwork Beyene

Reputation: 2337

How to get the index of a selected item in autocomplete?

I'm using the autocomplete feature of jQueryUI. It correctly gives list the items and I'm able to make a selection from the list. The problem I'm having is how to get the index of the selected item which is from an array used in the source event.

var options = {
    select: function() {
        // problem is here, I'm not able to see the correct index number of the selected item and always say -1
        alert($.inArray($("#searchAText").val()), arrayA); 
    },
    source: function(req, response) {
        var re = $.ui.autocomplete.escapeRegex(reg, term);
        var matcher = new RegExp("^" + re + "i");
        response($.grep(arrayA, function(item, index) {
            return matcher.test(item);
        })); 
    }
};
}

Upvotes: 2

Views: 2423

Answers (1)

ParPar
ParPar

Reputation: 7569

Try this:

select: function(event, ui) {
     alert($.inArray(ui.item.value, arrayA));
}

Upvotes: 4

Related Questions