I have an autocomplete field and have it’s source set to an array of friends.
GetFollowing(9, function(response)
{
if (response)
{
donor.following = response;
for (var i = 0; i < response.length -1; i++)
{
// make sure we have an image to display
if (response[i].image != null)
{
$("#following").prepend("<li title='" + response[i].firstname + " " + response[i].lastname +
"'><img src='<?php echo $baseUrl;?>/uploads/profile-pictures/" + response[i].image +
"' alt='" + response[i].firstname + " " + response[i].lastname + "'/></li>");
// add to the friends array
if (! $.inArray(response[i].firstname + " " + response[i].lastname, friends))
friends.push(response[i].firstname + " " + response[i].lastname);
}
}
}
});
GetFollowers(9, function(response)
{
if (response)
{
donor.followers = response;
for (var i = 0; i < response.length -1; i++)
{
// make sure we have an image to display
if (response[i].image != null)
{
$("#followers").prepend("<li title='" + response[i].firstname + " " + response[i].lastname +
"'><img src='<?php echo $baseUrl;?>/uploads/profile-pictures/" + response[i].image +
"' alt='" + response[i].firstname + " " + response[i].lastname + "'/></li>");
// Add to the friends array
friends.push(response[i].firstname + " " + response[i].lastname);
}
}
}
// setup jquery-ui autocomplete
$("#msg-to").autocomplete({source: friends});
});
My problem is I need to associate an id with the value that is selected, how can this be done?
http://jqueryui.com/demos/autocomplete/
you should read the overview more.
use the label for textual representation, the value for the id per item in the selection.
you should also differentiate the textbox used for the visual version (which displays words) from the data version (a hidden input which contains id’s) – similar to those of the datepicker widget (aka. alternate data). this can all be manipulated using the settings.