Skip to content

Instantly share code, notes, and snippets.

@maliMirkec
Created July 6, 2017 09:06
Show Gist options
  • Save maliMirkec/fd36aec429662ac4af26aece83ee0072 to your computer and use it in GitHub Desktop.
Save maliMirkec/fd36aec429662ac4af26aece83ee0072 to your computer and use it in GitHub Desktop.
Listen to click off element in vanilla JavaScript
var specifiedElement = document.getElementById('a');
//I'm using "click" but it works with any event
document.addEventListener('click', function(event) {
var isClickInside = specifiedElement.contains(event.target);
if (!isClickInside) {
//the click was outside the specifiedElement, do something
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment