Hide popup when clicking outside

//show popup when clicking the trigger
$('#trigger').on('click touch', function(){
  $('#tooltip').show();
});			

//hide it when clicking anywhere else except the popup and the trigger
$(document).on('click touch', function(event) {
  if (!$(event.target).parents().addBack().is('#trigger')) {
    $('#tooltip').hide();
  }
});

// Stop propagation to prevent hiding "#tooltip" when clicking on it
$('#tooltip').on('click touch', function(event) {
  event.stopPropagation();
});

Demo

Comments

  1. written by: Mohammad Anzar on August 25, 2017 at 1:00 pm - Reply

    Thank you so much. It worked for me

  2. written by: shailesh on September 5, 2017 at 11:31 am - Reply

    it’s working fine.

Leave a Reply

Your email address will not be published. Required fields are marked *

39 − = 38