Bootstrap typeahead - How to set the default value manually

19,499

Solution 1

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

Solution 2

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

$("#typeahead_object").val('test').focus().trigger('keyup')

Solution 3

As of typeahead.js 0.10.5, this works :

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

Share:
19,499
shinchilla
Author by

shinchilla

Updated on October 09, 2022

Comments

  • shinchilla
    shinchilla over 1 year

    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

  • shinchilla
    shinchilla over 10 years
    Hi. Thanks for answering. I tried it out and what it does is show the list of 'options'.
  • kayen
    kayen over 10 years
    Try triggering the change event then: $('input[data-provide="typeahead"]').val('test').trigger('ch‌​ange')
  • Prageeth godage
    Prageeth godage about 9 years
    after testing meny more finally your one helps me ... thanks Andrew for your answer.