How do I autocomplete on submit with twitter typeahead?

199 views Asked by At

When a hint is shown using twitter typeahead I would like that to replace the query when a user clicks submit.

Example the user types in mo, the hint shows montana and the user can click submit and montana is submitted.

This is the same functionality if a use presses the right arrow key.

1

There are 1 answers

0
Pedro Moreira On BEST ANSWER

I stored the last results from the typeahead and set the first value when submit is clicked. Probably there's a better way to do this, but didn't find any resource about how to do it.

http://jsfiddle.net/wLak8L20/1/

Filter

 filter: function (movies) {
            // Map the remote source JSON array to a JavaScript array
            window.lastMovies = movies;            
            return $.map(movies.results, function (movie) {
                return {
                    value: movie.original_title
                };
            });
        }

Submit callback:

jQuery('#submit').click(function() {
    try {
        $('.typeahead').val(lastMovies.results[0].original_title);
    } catch(e) {
    }
});