Check the exact div is clicked when a click triggered jQuery

Viewed 24
<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

1 Answers

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>

Related