javascript - Remove suggestions when inputfield is not focused -
i'm creating auto complete search box. suggestions have disappear when user clicks outside search field.
the form input field:
<input type="text" name="naam_textbox" id="naam_textbox" class="field" autocomplete="off" onkeyup="suggest(this.value, 'naam');" onblur="fill();" />
the javascript code:
function suggest(inputstring, veld){ if(veld == "naam") { if(inputstring.length < 2) { $('#suggestions_naam').fadeout(); } else { $('#naam_textbox').addclass('load'); var dropdown = $( '<div class="suggestionsbox" id="suggestions_naam" style="display: none;"><div class="suggestionlist" id="suggestionslist_naam"> </div></div>' ); if (inserted_naam == 0) { dropdown.insertafter( "#naam_textbox" ); inserted_naam = 1; } $.post("includes/homepage/autosuggest_naam.php", {querystring: ""+inputstring+""}, function(data){ if(data.length >0) { $('#suggestions_naam').fadein(); $('#suggestionslist_naam').html(data); $('#naam_textbox').removeclass('load') } }); } } } function fill(thisvalue, veld) { if(veld == "naam") { $('#naam_textbox').val(thisvalue); settimeout("$('#suggestions_naam').fadeout();", 60); } }
"fadeout" makes suggestions go away whenever inputstring less 2 characters, or when 1 of suggestions selected.
i need fadeout whenever clicks outside input field (lose focus) or doesn't select suggestion. make more clear, image:
the suggestions should go away when user clicks somewhere near red dot example. can't work, please?
here cause of issue,
onblur="fill();" //no parameters
whereas method
function fill(thisvalue, veld) { //2parameters if(veld == "naam") { $('#naam_textbox').val(thisvalue); settimeout("$('#suggestions_naam').fadeout();", 60); } }
missing pass parameters , therefore not working.
so change
onblur = "fill(this.value, 'naam')"
Comments
Post a Comment