How to get both id and value from typeahead.js when selecting a suggestion?
I have a json as follows:
[
{id:1, name:'paul'},
{id:2, name:'jim'},
{id:3, name:'tom'},
{id:4, name:'medor'},
{id:5, name:'janzy'}
]
and i created Bloodhound object as follows:
var bh = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
prefetch: '//localhost/data/names.json',
identify: function(datum){
return datum.id;
}
});
and i instantiated typeahead as follows:
$('#name').typeahead({"highlight":true}, {
"name":"name",
"source":bh,
"display":"value",
"limit":10
})
I want to be able to get the id
when i submit my form. When i submit the following example, i obtain only the name
attribute.
i tried to obtain the id via javascript as follows:
$('#name').bind('typeahead:select', function(ev, suggestion){
console.log(suggestion);
})
but suggestion
contains an object with only text value which i selected