DOM-Tricks

The good kind of manipulation

Detect clicks outside of an element

Detect clicks outside

To detect a click that occurs outside of an element, we add a click event listener to the entire document and then determine if the click also occurred within the target element.

In the code below outsideClick is set to a Boolean value which signifies a click outside of the elem has occurred.

document.addEventListener('click', function(event) {
  const outsideClick = !elem.contains(event.target);
});
Source for this page found on Github