Search code examples
jquerytwitter-bootstrap-3typeahead.jsbootstrap-typeaheadtwitter-typeahead

How to get reference to input element from within Bootstrap 3 Typeahead callback?


I'm trying to access the input element from within the Bootstrap 3 Typeahead (https://github.com/bassjobsen/Bootstrap-3-Typeahead) callback methods, but it seems that $(this), this, $(this)[0], and any other techniques described on this question, Get input element from Bootstrap typeahead , are not working, and there is nothing about this on the official documentation. This is very important if multiple instances of the Typeahead are initialized on one page using selectors that include more than one element, but only the relevant single element should be handled in each callback.

$(".typeahead").typeahead({
   afterSelect: function(item) {
      // the only parameter, item, is the JSON of the 
      // currently selected item, chosen from the list.
      // Neither $(this) nor this are references to the 
      // affected input element, so how do I access the 
      // input element from within this callback?
   },
   ...
});

Solution

  • within the function

    this.$element
    

    take a look at source code https://github.com/bassjobsen/Bootstrap-3-Typeahead/blob/master/bootstrap3-typeahead.js