How to exclude items from the click of the document

advertisements

jQuery

$(document).click( function () {
    alert("hi");
}

HTML

<div id="wrapper">
  <div id="inner-1">
    <div id="sub-inner1">Sub Inner1 Content</div>
    <div id="sub-inner2">Sub Inner2 Content</div>
    <div id="sub-inner3">Sub Inner3 Content</div>
  </div>

  <div id="inner-2" style="float:left; width:49%; border:dotted #CC3300;">
    content inside inner-2 div
  </div>
</div>

I want to show the Hi alert message on the Document click using Jquery. But I want to exclude the Inner-1 div and also its children. I meant that I want to get the alert message only when I click on the divs other than inner-1 and its children. Thanks.


Try

$(document).on('click', function (e) {
    if($(e.target).closest('#inner-1').length == 0){
        console.log("click", e.target);
    }
});

Demo: Fiddle