sergiodlopes / jquery-flexdatalist

Flexible input autocomplete/datalist plugin for jQuery
http://projects.sergiodinislopes.pt/flexdatalist/
MIT License
365 stars 84 forks source link

[feature] focusout event #210

Open ZTHawk opened 5 years ago

ZTHawk commented 5 years ago

Could you add a focusout event so we know when user stopped entering text.

LeonardoFeriato commented 3 years ago

Hi, I made this code for my case. Using a "multiple value" that allows only 1 value.

Change just #txtInput to your input id. <input type="text" class="sale-form-control flexdatalist" id="txtInput" name="txtInput" placeholder="Type here..." autocomplete="off"> $("#txtInput").flexdatalist({ limitOfValues: 1, multiple: true, minLength: 999 });

The code:

var controller = true; // Focus $(".flexdatalist-multiple").focusin(function() { // Lost focus $(".flexdatalist-multiple").focusout(function() { // Check if the Controller is ON and if the input has no value if (controller && $("#txtInput").val() == "") { $("#txtInput").val( // Set the value you entered in the input $("#txtInput-flexdatalist").val() ); controller = false; } else { // For some reason without this code below, to click on X doesn't work to remove value // Use the Remove Method to clear the entered value $("#txtInput").flexdatalist( "remove", $("#txtInput").val() ); controller = true; } }); });