Skip to content

Instantly share code, notes, and snippets.

@jp26jp
Created January 21, 2019 22:49
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 jp26jp/551fff1725463b2743f33d2cb55603d7 to your computer and use it in GitHub Desktop.
Save jp26jp/551fff1725463b2743f33d2cb55603d7 to your computer and use it in GitHub Desktop.
Hides a div when a click occurs outside of the specified container
$(document).click(function(e) {
let container = $("YOUR_CONTAINER_SELECTOR");
// if the target of the click isn't the container nor a descendant of the container
if (!container.is(e.target) && container.has(e.target).length === 0) {
container.hide();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment