I have seen this post here: jQuery UI autocomplete with item and id but I was not able to figure things out.
here is my input html:
<input type="text" class="tags" style="width:250px; height:24px;"> </input>'
<input type="hidden" name="tags_id" id="tags_id" />
here is my ajax call:
var data = {};
$.get('/tags',data, function(tag_list) {
autocomplete_source_list = [];
for(var i = 0; i < tag_list.length; i++){
autocomplete_source_list.push([tag_list[i].fields.display_name, [2,3,4,5,6,7,8,9,1,2]]);
}
jQuery( ".tags" ).autocomplete({
source: autocomplete_source_list,
select: function (event, ui) {
$(".tags").val(ui.item.label); // display the selected text
$(".tags_id").val(ui.item.value); // save selected id to hidden input
console.log("selected id: ", ui.item.label)
}
});
});
How do I set up the ids should I pass a 2d array to source? when I give the source to be just the text then both ui.item.value = ui.item.label = “whatever text”. I don’t see how id’s can be attached.
Can I get some help please. Thanks
From the fine manual:
So the
labelgoes in the drop down menu and thevaluegoes into the<input type="text">and you want something a little different, you want one thing in the<input type="text">and menu and another thing in a separate<input type="hidden">.Suppose you got some raw data like this back from your server:
Then you could build an array and a simple mapping object:
The
sourcearray would be given to.autocomplete()and yourselecthandler would usemappingto figure out what to put in the<input type="hidden">:Demo: http://jsfiddle.net/ambiguous/GVPPy/