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


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo