Source: https://stackoverflow.com/a/3028037
function hideOnClickOutside(element) { const outsideClickListener = event => { if (!element.contains(event.target)) { // or use: event.target.closest(selector) === null if (isVisible(element)) { element.style.display = 'none' removeClickListener() } } } const removeClickListener = () => { document.removeEventListener('click', outsideClickListener) } document.addEventListener('click', outsideClickListener) }
112800cookie-checkJavascript: Hide on click outside element