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?
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.
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.
Typeahead. A basic, easily extended plugin for quickly creating elegant typeaheads with any form text input.
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.
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; } });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With