javascript - typeahead.js : How to remove value when value not in suggestion -
i use typeahead.js
autocomplete textbox.
at first, when input , select value suggestions, textbox sets value correctly. then, input value not in suggestions , press tab , value of textbox doesn't clear.
how clear value of textbox when input value not in suggestions.
i ran same situation , way solved using events of typeahead.js
. record selection on typeahead:select
, check on typeahead:change
if selection made. if no selection made, reset input original value.
// initialize typeahead ususal var $mytypeahead = $("#my-typeahead"); $mytypeahead.typeahead(/* set-up typeahead here */); // set variables store selection , original // value. var selected = null; var originalval = null; // when typeahead becomes active reset these values. $mytypeahead.on("typeahead:active", function(aevent) { selected = null; originalval = $mytypeahead.typeahead("val"); }) // when suggestion gets selected save $mytypeahead.on("typeahead:select", function(aevent, asuggestion) { selected = asuggestion; }); // once user leaves component , change registered // check whether or not selected. if not reset // original value. $mytypeahead.on("typeahead:change", function(aevent, asuggestion) { if (!selected) { $mytypeahead.typeahead("val", originalval); } // selected value here needed... });
Comments
Post a Comment