Skip to content

Instantly share code, notes, and snippets.

@mrmolaei
Created December 2, 2022 18:53
Show Gist options
  • Save mrmolaei/544ff09b5fb44287fb166125464bbc57 to your computer and use it in GitHub Desktop.
Save mrmolaei/544ff09b5fb44287fb166125464bbc57 to your computer and use it in GitHub Desktop.
Detect click outside an element with Vanilla JS
// Check if the user clicked outside of an element
document.addEventListener('click', function (e) {
if (!e.target.closest('.element')) {
// code...
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment