osuthorpe
osuthorpe

Reputation: 255

How do I autocomplete on submit with twitter typeahead?

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.

Upvotes: 0

Views: 189

Answers (1)

Pedro Moreira
Pedro Moreira

Reputation: 975

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) {
    }
});

Upvotes: 1

Related Questions