Last active
March 11, 2023 15:20
-
-
Save everdimension/d43fecdbe9de637b5064 to your computer and use it in GitHub Desktop.
Listener for clicks outside the element, for example, for closing menus on outside click.
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
var el = document.getElementById('el'); | |
document.addEventListener('click', outsideEvtListener); | |
function outsideEvtListener(evt) { | |
if (evt.target === el || el.contains(evt.target)) { | |
return; | |
} | |
// code handling outside click | |
// removing the listener to avoid memory leaks | |
document.removeEventListener('click', outsideEvtListener); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment