How do I detect a click outside an element?

25/04/2019 | By Dev | Filed in: JavaScript.

You can listen for a click event on document and then make sure #dropdownmenu is not an ancestor or the target of the clicked element by using .closest().

For those who don’t want to use jQuery. Here’s the above code in plain vanillaJS (ECMAScript6).

Source: https://stackoverflow.com/questions/152975/how-do-i-detect-a-click-outside-an-element


Comments are closed here.