Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No free text with Typeahead

How can I disable free text input with Twitter's Typeahead plugin? All examples and configurations I find, the user can enter custom text. I need the user to NOT be able to enter custom text, but only if the text matches one of the options.

like image 453
lu1s Avatar asked Oct 01 '22 19:10

lu1s


1 Answers

The technique is to use .blur to check if valid suggestion is present

var myData = ["sugg1" , "sugg2" ]; // all data required for typeahead

$("#search").typeahead({source : myData})
                   .blur(validateSelection);

function validateSelection() {
    if(source.indexOf($(this).val()) === -1) 
        $("#search").val("");
}
like image 173
coder hacker Avatar answered Oct 10 '22 22:10

coder hacker