Getting index of selected item in jquery autoload

I am using jQuery autocomplete as follows:

From my PHP file, I get json encoded arrays - one for identifiers and one for names.

I fill in autocomplete with names. In the select function, I can correctly warn the selected item, but I cannot get the index of the selected item. How to get it?

 $(function() { $.ajax({ type: "POST", url: "get_data.php", success: function(data) { data_array = jQuery.parseJSON(data); $("#my_autocomplete").autocomplete({ source: data_array.names, select: function(event, ui) { alert(ui.item); } }); } }); }); 
+7
source share
1 answer

Example: http://jsfiddle.net/hY5Wt/

Instead of two arrays, you can have one array of objects. Each object has a label and an index: {label:"First", idx:1} . Autocomplete will use a shortcut to display and select an event, you can access ui.item.idx to get an identifier / index.

 $( ".selector" ).autocomplete({ source: [{label:"First", idx:1}, {label:"Second", idx:2}, {label:"Third", idx:3}], select: function(event, ui) { alert(ui.item.idx); } }); 

You are referring to an autocomplete plugin, but your code looks as if you are using jQuery UI.

+16
source

All Articles