Question

How do I get typeahead to submit data.

Here is the relevant code on jsfiddle http://jsfiddle.net/6W3Qu/2/.

A copy of the code:

var numbers = new Bloodhound({
datumTokenizer: function(d) { return Bloodhound.tokenizers.whitespace(d.num); },
queryTokenizer: Bloodhound.tokenizers.whitespace,
local: [
{ num: 'one' },
{ num: 'two' },
{ num: 'three' },
{ num: 'four' },
{ num: 'five' },
{ num: 'six' },
{ num: 'seven' },
{ num: 'eight' },
{ num: 'nine' },
{ num: 'ten' }
]
});

// initialize the bloodhound suggestion engine
numbers.initialize();

// instantiate the typeahead UI
$('.typeahead').typeahead(null, {
displayKey: 'num',
source: numbers.ttAdapter(),
updater: function(item) {
    alert(item);
}
});

It doesn't seem to enter the updater function

Was it helpful?

Solution

Typeahead.js doesn't have an "updater" option.

Instead you can write an event handler which will use typeahead's selected event (i.e. typeahead:selected) e.g.

var numSelectedHandler = function (eventObject, suggestionObject, suggestionDataset) {
    alert(suggestionObject.num);
};

typeahead.on('typeahead:selected', numSelectedHandler);

A working example can be found here:

http://jsfiddle.net/Fresh/TXQdy/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top