'Check the exact div is clicked when a click triggered jQuery

<div class="menu-options-label">Delete group</div>

Want to check the $(document).click() is this div click, If anywhere else is clicked alert('clicked somewhere else').Please guys appreciate your help thanks



Solution 1:[1]

To achieve this add the event handler to the document and interrogate the target property of the event that's passed to the handler to determine which element raised the event.

$(document).on('click', e => {
  let clickedElement = e.target;
  if ($(clickedElement).is('.menu-options-label')) {
    console.log('clicked element');
  } else {
    console.log('clicked somewhere else');
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<div class="menu-options-label">Delete group</div>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rory McCrossan