I'm using a piece of JS for an awesome little button I found online. The button works beautifully, but it doesn't hide the menu when I click on the page. I have to click the menu button to hide the menu again.
I've looked around a bit and see other threads like this, but my limited understanding of JS has me limited as to what I can do on my own.
$(function() {
var menuVisible = false;
$('.menuBtn').click(function() {
if (menuVisible) {
$('.myMenu').css({'display':'none'});
menuVisible = false;
return;
}
$('.myMenu').css({'display':'block'});
menuVisible = true;
});
$('.myMenu').click(function() {
$(this).css({'display':'none'});
menuVisible = false;
});
});
What do I add to this code, and how do I phrase it, to make the menu disappear when I click anywhere but the menu/button?
I only come here as a last resort. Everything I've tried breaks functionality. Thanks for your help! :)
.css({'display':'none'});use.hide()and.show(). In addition you can use.isto remember the visible state instead of using a variable. Remember DRY. Here's a cleaner way.