javascript - Close Autocomplete -


cannot work out doing wrong here. want autocomplete div close if there nothing in search field or when area of site clicked, similar onblur. instead div stays present. here code:

$(document).ready(function(){  $(".search").keyup(function()  { var searchbox = $(this).val(); var datastring = 'searchword='+ searchbox;  if(searchbox=='') { } else {  $.ajax({ type: "post", url: "search.php", data: datastring, cache: false, success: function(html) {  $("#display").html(html).show();       }  }); }return false;      }); }); 

"display" autocomplete div.

try --

see demonstration

var handler = function(event){    // hide "#display" on elsewhere click if($(event.target).not("#display *") || $(".search").val().length != 0) return;         $("#display").hide(); }  $(document).on("click", handler); 

above handler hide display div when cliked on elsewhere. if clicked inside display not hide.

and

$(".search").keyup(function(){      var searchbox = $(this).val();      if(searchbox.length === 0)  // if field blank hide div again.            $("#result").hide(); ....  // remaining code ---- 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -