Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Submit selection on Bootstrap typeahead() autocomplete?

How do I autosubmit the selection made with Twitter Bootstrap typeahead()??

http://twitter.github.com/bootstrap/javascript.html#typeahead

like image 421
Nathan Waters Avatar asked Feb 24 '12 03:02

Nathan Waters


People also ask

How do I get typeahead selected value?

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. Hope this will work for you.

What is typeahead bootstrap?

The typeahead input fields are very popular in modern web forms. The main purpose of using typeahead is to improve the user experience by supplying hints or a list of possible choices based on the text they've entered while filling a form or searching something — like the Google instant search.

What is type ahead functionality?

Typeahead is a feature of computers and software (and some typewriters) that enables users to continue typing regardless of program or computer operation—the user may type in whatever speed is desired, and if the receiving software is busy at the time it will be called to handle this later.


2 Answers

There is a clean way using the updater callback:

input.typeahead({
    'source' : ['foo', 'bar'],
    'updater' : function(item) {
        this.$element[0].value = item;
        this.$element[0].form.submit();
        return item;
    }
});

When user selects an option (either by mouse click or keyboard), the callback populates the input box and sends the form.

like image 72
erenon Avatar answered Oct 20 '22 18:10

erenon


If you use the external typeahead.js plugin (recommended for Bootstrap 3):

To trigger a form on select just use the custom events.

$('#my-input')
   .typeahead({/* put you options here */})
   .on('typeahead:selected', function(e){
     e.target.form.submit();
   });

More info on custom events here and demo about JSfiddle.

like image 41
HaNdTriX Avatar answered Oct 20 '22 18:10

HaNdTriX