0 points

Detect click outside element (vanilla JavaScript) - Stack Overflow


dashboard

Tue Jun 23 2020 09:26:58 GMT+0000 (UTC)

Posted by @mishka #javascript

var specifiedElement = document.getElementById(%27a%27);

//I%27m using "click" but it works with any event
document.addEventListener(%27click%27, function(event) {
  var isClickInside = specifiedElement.contains(event.target);

  if (!isClickInside) {
    //the click was outside the specifiedElement, do something
  }
});
content_copy Copy