JQuery: click * only * this item

Super-technical drawing

In the highly artistic drawing above, the green square is the baby pink. Pink is wrapped around green according to my function, so the green square can be anything - a hyperlink, image, button, etc.

I want to capture a click on the pink div ONLY if it is not a click on the green element either.

This can be done by transferring the logical value with the mouse center to the green square, but this seems like a messy way of doing it for me.

Any clues?

IMPORTANT CHANGE: I cannot communicate with the green square at all, so I am not adding anything to the click event.

+6
source share
5 answers

You can do it:

$('.pink-box-selector').click(function(e) { if ($(e.target).is('.pink-box-selector')) { // do stuff here } }); 
+8
source

Two options. You can first check if the target is a green div.

 $('#pinkdiv').click(function(e) { if ($(e.target).is('#greendiv')) return; // handle the event }); 

Or you can write a click handler for the pink div normally and stop the clicks on the green div from spreading.

 $('#greendiv').click(function(e) { e.stopPropagation(); }); 
+5
source

Did $("#div_id :not('#excluded_element')).click(); http://api.jquery.com/not-selector/

+1
source

Set up a separate event listener for the green element and let it event.preventDefault()

0
source

http://jsfiddle.net/rlemon/d8qVp/

 $("#pink").click(function(){ if(!$("#green").is(":hover")) { alert('here'); } }); 
0
source

Source: https://habr.com/ru/post/897321/


All Articles