jquerytwittertypeahead.jstwitter-typeahead

How to prevent Twitter typeahead from filling in the input with the selected value?


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?


Solution

  • 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("");
          });