Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent typeahead.js dropdown from closing on select

How can I prevent a typeahead dropdown from closing when an item is selected? I've tried using preventDefault like this:

$('#q').bind('typeahead:selected',function(obj, datum, name) {  
            ...
            obj.preventDefault();
        });

But no success.

Edit: I've managed to "fix" this by building Typeahead with lines 217-218 commented from typeahead_views.js:

  byClick && utils.isMsie() ?
       utils.defer(this.dropdownView.close) : this.dropdownView.close();

But there has to be another way without modifying source files?

like image 208
bionut Avatar asked Nov 11 '22 17:11

bionut


1 Answers

Had the same problem and the (very easy) solution doesn't seem to be documented anywhere

$(document).on('typeahead:beforeclose', function(event, data) {
  event.preventDefault()
})

(this just prevents the dropdown from closing at all which can be very helpful during development, use 'typeahead:beforeselect' if you want to prevent closing just on selet).

like image 112
SColvin Avatar answered Nov 15 '22 12:11

SColvin