I have a search suggestion box that appears when a user types in the search box. I want to close that box when the user clicks outside of the parent div. I tried this method below using .not()
to omit the parents div but it did not omit it and still closes it when clicked on the parent div.
I have looked at solutions using event propagation but those have their own perils so I'd really like to use a simple function like .not() to accomplish this.
JS
// close search suggestions
$("body").not('#searchbox').mousedown(function() {
$('.search-suggestions').fadeOut();
});
HTML
<section id="searchbox">
<div class="container">
<div class="row">
<div class="col-md-12">
<form class="form-inline clearfix">
<div class="form-group pull-left">
<input type="text" class="form-control" id="search-input" placeholder="Search">
</div>
<button type="submit" class="btn btn-default pull-right"><i class="fa fa-search"></i></button>
</form>
</div>
<div class="col-md-12 search-suggestions">
<ul class="list-unstyled">
<li><h4>Browse:</h4></li>
<li class="suggestion-new-homes"><a href="#"><span class="suggestion-result">result</span> in New Homes</a></li>
<li class="suggestion-renovation"><a href="#"><span class="suggestion-result">result</span> in Renovation</a></li>
<li class="suggestion-interior-living"><a href="#"><span class="suggestion-result">result</span> in Interior Living</a></li>
</ul>
</div>
</div>