Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bootstrap-typeahead.js add a listener on select event

I'm new to the Bootstrap Twitter framework, and I need to use bootstrap-typeahead.js for autocomplete but I need also to get the value selected by the user for the typeahead.

This is my code:

<input type="text" class="span3" style="margin: 0 auto;"                     data-provide="typeahead" data- items="4"                     data-source='["Alabama","Alaska"]'> 

How can I add a listener to get the selected value from type typeahead and pass it in a function? For example when I use ExtJs, I apply this structure:

listeners: {    select: function(value){      ........    } } 

How can I do something similar with a typeahead?

like image 761
jack.cap.rooney Avatar asked Feb 29 '12 09:02

jack.cap.rooney


People also ask

How do I use Typeahead select?

typeahead( When text is entered, it fires the source: option to fetch the JSON list and display it to the user. If a user clicks an item (or selects it with the cursor keys and enter), it then runs the updater: option. Note that it hasn't yet updated the text field with the selected value.

How do I get Typeahead selected value?

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. Hope this will work for you.

What is Typeahead bootstrap?

Typeahead. A basic, easily extended plugin for quickly creating elegant typeaheads with any form text input.

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

you should use "updater":

$('#search-box').typeahead({      source: ["foo", "bar"],      updater:function (item) {          //item = selected item         //do your stuff.          //dont forget to return the item to reflect them into input         return item;     } }); 
like image 167
Capy Avatar answered Oct 01 '22 21:10

Capy