I used the “Custom data and display” example at http://jqueryui.com/demos/autocomplete/ and simplified it a bit so I can have values and ID’s like so:
$(function() {
var prd = [
{ s: "hallo", v: "1" },
{ s: "hey", v: "2" },
{ s: "alloh", v: "3" }
];
$("#product").autocomplete({
source: prd,
minLength: 0,
focus: function(event, ui) {
$("#product").val(ui.item.s);
return false;
},
select: function(event, ui) {
$("#product").val(ui.item.s);
$("#productId").val(ui.item.v);
return false;
}
}).data("autocomplete")._renderItem = function(ul, item) {
return $("<li></li>")
.data("item.autocomplete", item)
.append("<a>" + item.s + "</a>")
.appendTo(ul);
};
});
The problem is that without the minLength:0 it will never ever pop up, and with the minLength:0 it only pops up with an empty textbox (type a char and then delete it).
Jqueryui autocomplete’s use of “label” and “value” is almost automagical. Change your ‘s’ and ‘v’ back to ‘label’ and ‘value’ and everything will work as expected: