I have the following js code that prepares a search field using Twitter Typeahead :
var users = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('cname'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: 'application/controller/TypeaheadSeed.php?QUERY=%QUERY'
});
users.initialize();
$('#input-friend-list-publication').typeahead({
hint: true,
highlight: true
}, {
name: 'users',
displayKey: 'cname',
source: users.ttAdapter(),
templates: {
header: '<div class="tt-header">Meus amigos</div>',
empty: [
'<div class="empty-message">',
' Não foi possível encontrar resultados para essa pesquisa.',
'</div>'
].join('\n'),
suggestion: Handlebars.compile(
'<div class="result-values" style="display: inline-block; vertical-align: top;" data-value="{{username}}">' +
' <img class="img-rounded" src="{{profilepicpath}}" alt="{{cname}}" title="{{cname}}" width="48" height="48" />' +
'</div>' +
'<div style="display: inline-block; vertical-align: top; margin-left: 10px">' +
' <div>{{cname}}</div>' +
'</div>')
}
});
How do I get the value of the data-value
attribute? Even tried a solution using the bind()
function but without success, see below.
$('#input-friend-list-publication').bind('typeahead:selected', function () {
console.log($(this));
});
Note: My problem is not to get a value of an attribute data-
itself.