title: Use JQuery to listen for all clicks on the html body Except a specific div and its children
body: <p>I am trying to use a jQuery listener to listen for a users clicks on the html body and perform a specific function if anywhere on the body has been clicked except for a specific div and the children within that div. </p>
  
  <p>The idea is that the div is a popup type element and instead of having to have a close button that the user can click, they should just be able to click anywhere on the page besides that div and it will automatically close.</p>
  
  <p>I have been using this listener:</p>
  
  <pre><code>var initialClick = false;
  $('body').on('click.addPhoneListeners', function(event) {
      var target = EventUtility.getTarget(event);
      if(initialClick) {
          if(target.parentNode.id != clone.id &amp;&amp; target.id != '') {
              finish();
          };
      }
      initialClick = true;
  });
  </code></pre>
  
  <p>Which listens for all clicks on the body and unless the click comes from within the element I want the user to be able to interact with, it closes. Unfortunately this only works with a div that has only one level of children. As soon as I start getting multiple hierarchies such as this:</p>
  
  <pre><code>&lt;div id="addressContainer"&gt;
      &lt;div id="address" class="hidden row"&gt;
          &lt;div class="row"&gt;
              &lt;div id="address.primary" class="hidden"&gt;P&lt;/div&gt;
              &lt;div id="address.type"&gt;&lt;/div&gt;
              &lt;div id="address.street"&gt;&lt;/div&gt;
              &lt;div id="address.editButton" class="hidden"&gt;&lt;a id="addressEditButton"&gt;Edit&lt;/a&gt;&lt;/div&gt;
              &lt;div id="address.deleteButton" class="hidden"&gt;&lt;a id="addressDeleteButton"&gt;Delete&lt;/a&gt;&lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="row"&gt;
              &lt;div id="address.city"&gt;&lt;/div&gt;
              &lt;div id="address.state"&gt;&lt;/div&gt;
              &lt;div id="address.zip"&gt;&lt;/div&gt;
          &lt;/div&gt;
          &lt;input type="hidden" id="address.id"&gt;&lt;/input&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>The target.parentNode.id gives me the objects parent element as opposed to the addressContainer id and thus does not work. Is use the top level parent from within nested elements? Other elements will be using this same code, so it has to work on both divs with just one level and div's with multiple.</p>
  
  <p>UPDATE: Found a few excellent solutions, thanks guys. I do however have one other question. Refer to my code above where I set an initialClick boolean to false, then set it to true. I am doing this because for some reason if I don't, when I go to add the popup div, the initial click from the button used to set that popup fires the listener and closes the popup before I have a chance to do anything. This has been my solution around the problem, but is that the only way? Or am I just setting the listener slightly incorrect?</p>