document click hide popup jquery code example

Example: document click hide popup jquery

//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();
});

Tags:

Misc Example