In a recent project, I had to deal with this task – writing and event handler to detect click events outside an element. The first thing that came to mind was listening for click events somewhere higher in the DOM hierarchy ( can be parent element, or the window object for example ). Then, I can determine the element that triggered the event and all the elements, through the event bubble .
Here is the final result:
See the Pen Click outside element by Chrysto (@burnandbass) on CodePen