How do I use jQuery this and class to affect a div when the user clicks outside the div
<p>How do I use jQuery this and class to affect a div when the user clicks outside the div</p>

<p>I have a search that I built that works. I'm stuggling with getting the results to erase the results Only when the user clicks outside of the results or the search box. The following works with the exception that it wipes out the results even if I click IN the search box or the results div. (I imagine that the issue is related to "this" reference in the if statement.)</p>

<pre><code>$('#searchbox').keyup(function(){
  $.post("remote.php",{'func':'contactsearch','partial':this.value},function(data){
    $("#results").html(data);
     // erase the results when user clicks outside the search
     $('body').click(function(){
       if (!$(this).hasClass('nd_search')) { // erases results even when clicked inside result - why?
         $("#results").html('');
         $('body').unbind('click'); // remove event handler. add again when results shown.
        }
    });
   });
  });       
});

&lt;div class="nd_search"&gt;
    &lt;input id="searchbox" type="text" class="nd_search"/&gt;
    &lt;div id="results" class="nd_search"&gt;&lt;/div&gt;
&lt;/div&gt;
</code></pre>

<p>I also tried:</p>

<pre><code> $('body').click(function(event){
    if (!$(event.target).hasClass('nd_search')) { ...
</code></pre>

<p>Secondarily, I would rather have the class on only the containing div. How would I change the if statement? </p>

<p>I looked at the other posts about this subject, which got me this far. I'm almost there</p>

<p>Thanks</p>
