Created
December 2, 2022 18:53
-
-
Save mrmolaei/544ff09b5fb44287fb166125464bbc57 to your computer and use it in GitHub Desktop.
Detect click outside an element with Vanilla JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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