I've searched through all the current answers for others who have asked this question and nothing is working for me after trying numerous sample code, so I figured it was time to break down and ask. Forgive me if this is repeating of a question but since I've not had success with other variants of this I thought it would be OK.
I'm using Jquery to bring up a DIV containing a list items in my database. I'd like for that DIV to disappear when I click outside of it. This is the code I used.
<script type="text/javascript" src="https://ajax.googleapis.com /ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
function searchq(){
var searchTxt = $("input[name='search']").val();
$.post("live_search.php", {searchVal: searchTxt}, function(output) {
$("#output").html(output);
});
}
</script>
<script type="text/javascript">
$("#auto").autocomplete({
source: function(request, response) {
var results = $.ui.autocomplete.filter(myarray, request.term);
response(results.slice(0, 10));
}
});
</script>
///////////
MY ATTEMPT TO HIDE THE DIV
<script type="text/javascript">
$(document).click(function(e) {
if(!$(e.target).closest('#output').length){
$("#output").hide();
}
});
</script>
The DIV i'm using to show the data is named output, as shown in the code. Any ideas on how I can get the output DIV to disappear when clicking outside of the output DIV?
Kind Regards Sour Jack