Skip to content

Instantly share code, notes, and snippets.

@logeshpaul
Created May 7, 2014 10:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save logeshpaul/7ff4283744c3d0b05ad4 to your computer and use it in GitHub Desktop.
Save logeshpaul/7ff4283744c3d0b05ad4 to your computer and use it in GitHub Desktop.
Detect if clicked/touched outside some block
// 'touchstart' helps trigger the event in touch devices
$(document).on('click touchstart', function(e){
if ($(e.target).closest("#menuscontainer").length == 0) {
// .closest can help you determine if the element
// or one of its ancestors is #menuscontainer
console.log("hide");
}
});
$('#menuscontainer').on('click', function(e){
e.stopPropagation();
e.preventDefault();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment