2

I have a situation where I need to fire an event when right clicked on a div with class types_list_button, but within that div is another div with the class hover_point in which I do not want the event to fire when they are clicking over that div. Is this possible?

I am not sure if I can use :not with this type of event handler I am using.

HTML

<div class="types_list_button">Any where here<div class="hover_point">Not Here</div>Any where here</div>

JQUERY

$(document).on('contextmenu', '.types_list_button', function() {

$(document).on('contextmenu', '.types_list_button', function() {
  alert("test")
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="types_list_button">Any where here<div class="hover_point">Not Here</div>Any where here</div>
isapir
  • 21,295
  • 13
  • 115
  • 116
Cesar Bielich
  • 4,754
  • 9
  • 39
  • 81

2 Answers2

2

Just check for the class in the target of the Event object inside the event handler:

$(document).on("contextmenu", ".types_list_button", function(evt) {
  if (!$(evt.target).hasClass("hover_point"))
    alert("test");
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="types_list_button" style="margin:1em; padding:1em; background:#fcc;">Anywhere here
  <div class="hover_point" style="margin:1em; padding:1em; background:#cfc;">Not Here</div>
Anywhere here</div>
isapir
  • 21,295
  • 13
  • 115
  • 116
0

How about...

$(document).on('contextmenu', '.types_list_button', function() {
  alert("test")
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="types_list_button">
Any where here
</div>
<div class="hover_point">
Not Here
</div>
<div class="types_list_button">
Any where here
</div>
suchislife
  • 4,251
  • 10
  • 47
  • 78