Detect clicks outside of an element

Check if a click was outside of the ele element:

document.addEventListener('click', function(evt) {
const isClickedOutside = !ele.contains(evt.target);

// `isClickedOutside` is true if the clicked target is outside of `ele`
});

Use case

More

Previous
Create an element
Next
Detect if an element is focused