I'm using Twitter's typeahead plugin for autocompletion with a text input. I want to change the behavior so that instead of putting the selected option's value into the text input (when clicked), it does something else with it.
Is there any way to prevent typeahead from automatically filling in the input with the selected option's value?
Struggled with this for a while, before I found how to clear the text when you select an option:
$("#typeahead-input").on('typeahead:selected', function (event, datum, name) {
$(this).typeahead("val", "");
// Do something with the datum...
});
I tried every other thing under the sun, but just stumbled across a solution a few minutes after posting my question.
In case anyone else needs help with this, adding the typeahead:closed
event handler allows you to catch the entered value, as well as clear it from the input before it can be seen (maybe there's a way to prevent it from ever being entered into the input, but I haven't been able to find it).
So here's what I'm doing:
$("#input").typeahead({
highlight: true
},
{
name: 'my-dataset',
displayKey: 'value',
source: bloodHound.ttAdapter()
}
).on('typeahead:closed', function (obj, datum, name) {
$(obj.currentTarget).val("");
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With