Question

For technical reasons I'm using Typeahead 0.9.3. I would like to track clicks and submits for a Twitter Typeahead-powered search box. I need to trap these things: the submit event and the search term, or the highlighted suggestion's url, matched token, and suggestion value, whether triggered by click or return.

The TypeaheadView._handleSelection method has the information I need and I'd prefer not to hack the plugin to get it: https://github.com/twitter/typeahead.js/blob/v0.9.3/dist/typeahead.js#L980

My question: is there a way to use the Typeahead instance's methods to find this information? I looked through the DOM and couldn't find an instance of Typeahead. Am I missing it, or is there another way to call and access it?

Was it helpful?

Solution

You can get access to the TypeaheadView instance with $('#myTypeaheadInput').data('ttView'). However, instead of trying to work with TA internal methods directly I'd suggest creating an event handler for suggestionSelected:

$('#myTypeaheadInput').data('ttView').dropdownView.on('suggestionSelected', function(evt) {
    console.log(evt.data);
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top