Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery: get mouse click if inside a div or not

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

like image 761
trrrrrrm Avatar asked Feb 15 '10 10:02

trrrrrrm


People also ask

How can I tell if my cursor is inside a div?

You can simply use the CSS :hover pseudo-class selector in combination with the jQuery mousemove() to check whether the mouse is over an element or not in jQuery. The jQuery code in the following example will display a hint message on the web page when you place or remove the mouse pointer over the DIV element's box.

What is Click () method?

click() method simulates a mouse click on an element. When click() is used with supported elements (such as an <input> ), it fires the element's click event. This event then bubbles up to elements higher in the document tree (or event chain) and fires their click events.

How can you tell if a mouse is left or right jQuery?

Using the mousedown() method: The mousedown() method in jQuery can be used to bind an event handler to the default 'mousedown' JavaScript event. This can be used to trigger an event. The event object's 'which' property can then used to check the respective mouse button.

What is click event in JavaScript?

An element receives a click event when a pointing device button (such as a mouse's primary mouse button) is both pressed and released while the pointer is located inside the element.


1 Answers

$("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
});
like image 198
harpax Avatar answered Nov 04 '22 15:11

harpax