Preview:
let specifiedElement = document.getElementById('a');
 
//I'm using "click" but it works with any event
document.addEventListener('click', function(event) {
  let isClickInside = specifiedElement.contains(event.target);
 
  if (!isClickInside) {
    //the click was outside the specifiedElement, do something
  }
});
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter