Bootstrap just implemented a neat autocomplete feature called typeahead. I am having trouble getting the appropriate value for the text input.
For instance, I might have the following:
$('input').on('change', function(){
console.log($(this).val());
});
This does not appear to capture the selected value. Does anyone know how to get the selected value using typeahead with Bootstrap?
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.
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.
When users need to select from a long list of options, use the dropdown typeahead. As soon as the user starts typing, the list changes to show suggestions that should get closer to what the user wants.
What is Typeahead? Typeahead - also known as autocomplete or autosuggest - is a language prediction tool that many search interfaces use to provide suggestions for users as they type in a query.
$('#autocomplete').on('typeahead:selected', function (e, datum) {
console.log(datum);
});
$('#autocomplete').on('typeahead:cursorchanged', function (e, datum) {
console.log(datum);
});
Seems Typeahead changed listeners and usages are not well documented. You can use these listeners.
UPDATE
Event name was changed to typeahead:select
in later versions.
You're looking at it from the wrong angle. Bootstrap's typeahead method provides an option which allows you to pass a callback function that receives the selected value as the argument. Below is a condensed example illustrating just this one argument.
$('#typeaheadID').typeahead({
updater: function(selection){
console.log("You selected: " + selection)
}
})
cheers! documentation refernece
I'm working with this and I'm having the same issue right now. I'm planning to solve it with this pretty fork
https://gist.github.com/1891669
And I did it with a callback:
$('.typeahead').typeahead({
// note that "value" is the default setting for the property option
source: [{value: 'Charlie'}, {value: 'Gudbergur'}, ...],
onselect: function(obj) { console.log(obj) }
})
Hope it helps you too.
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