html - Javascript - Click outside of element using event.target not working -
this question has answer here:
i have a pop menu has close button works fine. i'm trying close menu when user clicks outside of box too.
i have tried using if statement inside closemenu() checks if event.target wasn't button element doesn't seem work.
any ideas or direction appreciated.
var button = document.createelement('div'); button.classname = "doorselector--familyoption"; var event_func = (parent == null) ? this.closemenu.bind(this, close_container) : this.createmenuoptions.bind(this, parent, null); button.addeventlistener('click', event_func); menu.prototype.closemenu = function(container, event) { event.preventdefault(); event.stoppropagation(); container.classlist.remove('show'); };
the full code can seen here if needed.
thanks
you add click-listener on button
. callback function closemenu
invoke if button clicked, if click outside nothing happens. should add event listener on "outside" :) on <body>
example