var box = document.querySelector(".box");
// Detect all clicks on the document
document.addEventListener("click", function(event) {
// If user clicks inside the element, do nothing
if (event.target.closest(".box")) return;
// If user clicks outside the element, hide it!
box.classList.add("js-is-hidden");
});
I have something like this where this.bodyElement is just the body tag and this.closeSidebar() a function that removed classes (in my case it’s a sidebar overlay instead of a modal but does the same thing.
@pasacallaliberte the only issue seems to be that I can’t click an element inside the dropdown (I have an input) without triggering the hide - any suggestions?
e.g. code is here: https://codepen.io/askalot/pen/PQmEXL - but I am unable to get it to work as the static target = [‘menu’,‘button’] line is throwing an error in CodePen.