Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typeahead.js get selected datum

I'm trying to use Twitter typeahead.js to return a datum after selection. From what I understood by the documentation the code should look something like

$('.selector').typeahead({
  name: 'identifier',
  local: localObjectsArray
}).on('autocompleted', function(item){
    alert(JSON.stringify(item));
});

However this doesn't work. What's the proper way to detect typeahead events?

like image 288
Sovos Avatar asked Aug 02 '13 14:08

Sovos


People also ask

How do you get the value of Typeahead selected?

typeahead({ itemSelected:function(data,value,text){ console. log(data) alert('value is'+value); alert('text is'+text); }, //your other stuffs }); You have to just pass itemSelected in the callback function and it will give you the selected item.

What is Typeahead jQuery?

jQuery plugin that provides Typeahead (autocomplete) Search preview from Json object(s) via same domain Ajax request or cross domain Jsonp and offers data compression inside Local Storage. The plugin is built with a lot of options and callbacks to allow customization.


1 Answers

After selection, so you need typeahead:selected:

$('.selector').typeahead({
    name: 'identifier',
    local: localObjectsArray
}).on('typeahead:selected', function (obj, datum) {
    console.log(obj);
    console.log(datum);
});

Hope it helps.

like image 77
Hieu Nguyen Avatar answered Jan 04 '23 03:01

Hieu Nguyen