An autocomplete widget expects a data source in array format using:
- Objects that contain the label property, the value property , or both
- Simple String Values
You are currently creating a second (array of string values) that works fine, but you can also slightly customize your data when you iterate over it, and also supply other properties of the object:
$("#auto_cp").autocomplete({ minLength: 3, //source source: function(req, add) { $.getJSON("friends.php?callback=?", req, function(data) { var suggestions = []; $.each(data, function(i, val) { suggestions.push({ label: val.name, zzz: val.zzz }); }); add(suggestions); }); }, //select select: function(e, ui) { alert(ui.item.zzz); } });â
Now, since the array that you supply the widget contains objects with the name property, you must get the autocomplete functionality and also access the zzz
property.
Here is a working example (without calling AJAX): http://jsfiddle.net/LY42X/
source share