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 --
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
Post a Comment