Home > database >  Check the exact div is clicked when a click triggered jQuery
Check the exact div is clicked when a click triggered jQuery

Time:05-03

<div >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

CodePudding user response:

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 >Delete group</div>

  • Related