jquery: get mouse click if inside a div or not

trrrrrrm picture trrrrrrm · Feb 15, 2010 · Viewed 33.5k times · Source

i have this HTML page

<html>
<body>
<div>a</div>
<div>b</div>
<div>c</div>
<div>d</div>
<div id='in_or_out'>e</div>
<div>f</div>
</body>
</html>

a,b,c,d,e and f could be divs also not just a plain text.

I want to get the mouse click event, but how could i know if it's inside or outside #in_or_out div ?

EDIT :: guys, i know how to check if the div is click or not, but i want my event to be fired when the click is outside that div

Answer

harpax picture harpax · Feb 15, 2010
$("body > div").click(function() {
    if ($(this).attr("id") == "in_or_out") {
        // inside
    } else {
        // not inside
    }
});

EDIT: just learned, that there is a negate:

$("body > div:not(#in_or_out)").click(function(e) {
    // not inside
});