Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get selected items value for Bootstrap's typeahead

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?

like image 734
T.S. Avatar asked Feb 27 '12 02:02

T.S.


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.

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.

What is typeahead dropdown?

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 field?

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.


3 Answers

$('#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.

like image 172
ymutlu Avatar answered Oct 02 '22 21:10

ymutlu


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

like image 42
Secesh Avatar answered Oct 02 '22 21:10

Secesh


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.

like image 35
vanhalt Avatar answered Oct 02 '22 21:10

vanhalt