I wrote some auto complete code who works, the list is showing up, but now I need to make those clickable, I have an ID stored in the JSON who I call over ajax, but I can't get it in the 'select' section from the autocomplete, so I declared an array and filled it with the IDs in the 'source' mapping section. But I need to know the index number of the clicked/selected suggestion.
Here is my code:
// Declarations
var i = 0;
var j = 0;
var ids = []; // Array of ids
$('#search-bar').autocomplete(
{
source : function(requete, reponse){
$.ajax(
{
url : 'search/' + $('#search-bar').val(), // JSON source
dataType : 'json',
success : function(data){
reponse($.map(data, function(objet)
{
ids[i] = objet.id; // Filling up the array
i++;
return objet.name;
}));
}
});
},
select: function (event, ui)
{
window.location = 'project/' + ids[j]; // Link the the project page
j++; // not working
}
});
Here is an example of returned JSON:
[{"id":1,"name":"Project 1"},{"id":2,"name":"Project 2"}]
Someone knows how to replace my 'j' variable with the index of the clicked/selected entry ?