views:

63

answers:

2

I want to have a click event on the body tag, but I have a div under the body tag which i dont want to have the click event on. I have tryed with this but that doesent seem to work correct:

$("body").not("#InnerDiv").click(function() {
    alert("Hejhej");
});

The html:

<body>
   <div id="1">1</div>
   <div id="2">2</div>
   <div id="InnerDiv">InnerDiv</div>
</body>
+1  A: 

Try


    $("body").click(function() { alert("Hejhej"); });
    $('#InnerDiv').click(function(e) { e.stopPropagation(); });
Dave
+5  A: 

Click events bubble up. The default click handler on InnerDiv delegates to the click event of its parent. You can override that event and ask it not to bubble up.

$("body").click(function() {
          alert("Hejhej");
      });
$("#InnerDiv").click(function(e) {
          e.stopPropagation();
      });
jes5199
stopPropagation... that was what I was looking for, I couldn't find it in the documentation
Dave
yeah, jQuery's documentation on these methods was really hard to find.
jes5199