Hide Menu When Clicked Outside

I've written a code that hides my menu when clicked outside of the menu or the button. Do you think my code is good or it can be improved. New to Javascript. https://codepen.io/Arslan-Akbar/pen/poOMadb
2 Replies
Malik
MalikOP2y ago
nav is the .navigation & collapse_btn is the .nav-toggle-btn.
~MARSMAN~
~MARSMAN~2y ago
I would use blur event listener It fires Whenever the element loses its focus state. But if what you did is working for you then great. I honestly see it working well. https://developer.mozilla.org/en-US/docs/Web/API/Element/blur_event
Element: blur event - Web APIs | MDN
The blur event fires when an element has lost focus. The event does not bubble, but the related focusout event that follows does bubble.
Want results from more Discord servers?
Add your server