Add a click listener to fire a callback for everywhere on the window except your chosen element.
- run
npm install @lukeboyle/when-clicked-outside
// ES6
import whenClickedOutside from '@lukeboyle/when-clicked-outside';
// CommonJs
const whenClickedOutside = require('@lukeboyle/when-clicked-outside');
whenClickedOutside('#element', whenClickedOutsideCallback, {});
function whenClickedOutsideCallback(event) {
console.log('hello');
}
// ES6
import whenClickedOutside from '@lukeboyle/when-clicked-outside';
// CommonJs
const whenClickedOutside = require('@lukeboyle/when-clicked-outside');
const listener = whenClickedOutside('#element', whenClickedOutsideCallback, {});
function whenClickedOutsideCallback(event) {
console.log('hello');
listener.destroy();
}
Type: querySelector String OR HTMLElement
e.g. 'body', '.class-name', '#id', document.getElementById('id')
Type: Function
Type: Object
To set the standard addEventListener, add an 'options' element to the config object
Returns an object with a reference to your element, and a destroy function to remove the listener.
- fix: add missing dependency
- fix: remove reference to window.event to address a bug in firefox
- fix: add explicit reference to the
MouseEvent
in click verification function
- docs: update the readme with commonJs require
- fix: build issue when publishing
- breaking change: Export whenClickedOutside by default
- refactor: tidy verifyElementExists function
- fix: add error handling for incorrect types
- fix: add error handling for non-existent elements