Bootstrap typeahead - how to set the default value manually

I tried this:

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

It only changes the value of the text field, but does not call the "updater" function of type head.

Question: How to set the value of a typeahead object manually and run its update function?

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

+7
jquery bootstrap-typeahead
source share
4 answers

To change the input value of typeahead, use the following construct:

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

So, in your case, it should look like this:

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

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

+13
source share

You can manually trigger the keyup event on the input element after setting the value:

 $("#typeahead_object").val('test').focus().trigger('keyup') 
+1
source share

Like typeahead.js 0.10.5, this works:

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

0
source share

I also have this question, but I want to show the name and select by ID

I have this data:

 [ { id: 100, name: 'name100'}, { id: 101, name: 'name101'} ] 

with this config:

 jQuery(".typeahead").typeahead({ highlight: false, hint: false, minLength: 2 }, { name: 'test1', display: 'name' }); 

I want to select the element with identifier 101, "val" just set the value of the input field and show "101" instead of "name101"

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

https://jsfiddle.net/n03cfgej/3/

How can I choose a product by ID?

0
source share

All Articles