Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap typeahead - How to set the default value manually

I tried this:

$("#typeahead_object").val("test");

It only changes the textbox value but doesn't trigger the 'updater' function of the typeahead.

Question : How can I set the value of a typeahead object manually and trigger its updater function?

http://getbootstrap.com/2.3.2/javascript.html#typeahead

like image 228
shinchilla Avatar asked Aug 19 '13 08:08

shinchilla


People also ask

What is typeahead dropdown?

To help a user make a selection in a long list, a dropdown typeahead shows suggestions as soon as the user starts typing.

What is typeahead bootstrap?

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

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.

What is typeahead API?

Performs search to retrieve list of places by input text and location vicinity. Address Autocomplete takes free form text as input. It could be a part of an address. Location could be provided either as latitude/longitude or as country ISO code. It returns consolidated list of addresses based on the input text.


3 Answers

As of typeahead.js 0.10.5, this works :

$('#typeahead_object').typeahead('val', 'some value').blur()

like image 134
Jim Avatar answered Sep 21 '22 23:09

Jim


To change typeahead input value, use the following construction:

$(input).typeahead('val',value);

So, in your case it should looks like:

$("#typeahead_object").typeahead('val',"test");

typeahead API documentation is available on https://github.com/twitter/typeahead.js/blob/master/doc/jquery_typeahead.md#datasets

like image 36
Andrew Avatar answered Sep 23 '22 23:09

Andrew


You could manually trigger a keyup event on the input element, after setting the value:

$("#typeahead_object").val('test').focus().trigger('keyup')
like image 27
kayen Avatar answered Sep 22 '22 23:09

kayen