Defocuser

Utility for detecting clicks outside of an element

Get Started

Simplicity

Quick setup, clear API

Advanced features

Works good out of the box and allows customization when you need it

Small

Build for both direct in-browser usage and as an NPM module for inclusion in your project

It's easy!

npm install --save-dev defocuser

or

<script src="https://unpkg.com/defocuser"></script>

and then use it on the element you want to track outside clicks of:

<div id="my-dropdown">....</div>

const defocuser = new Defocuser()
const el = document.querySelector('#my-dropdown')
defocuser.addElement(el, 'bubbling', () => console.log('Clicked outside of #my-div'))