There is a menu and the behavior for clicks in different places, but only the function that opens the menu when you press the button works. Closing the same menu when clicking on the same button does not work. I don't know, maybe I wrote the wrong condition for closing. I can not understand. Where am I wrong?
function hideMenu(menuId) {
if ($(menuId).is(":visible")) {
$(menuId).hide();
} else {
$(".hiddenMenu:visible").hide();
$(menuId).show('800');
}
$(document).mouseup(function(e) {
var div = $(menuId);
if (!div.is(e.target) && div.has(e.target).length === 0) {
div.hide();
}
});
console.log(menuId);
}
body {
margin: 0;
padding: 0;
background: #040921;
}
.container {
width: 80px;
height: 100vh;
background: #263154;
position: absolute;
display: flex;
flex-direction: column;
}
.container nav ul li a {
cursor: pointer;
}
#submenuId1 {
position: absolute;
height: 100vh;
top: 0;
left: 80px;
background: orange;
display: none;
}
#submenuId2 {
position: absolute;
height: 100vh;
top: 0;
left: 80px;
background: yellow;
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='container'>
<nav>
<ul>
<li><a onclick="hideMenu('#submenuId1')">Link</a></li>
<li><a onclick="hideMenu('#submenuId2')">Link</a></li>
</ul>
</nav>
</div>
<div id="submenuId1" class="hiddenMenu">
<nav>
<ul>
<li>subMenuItem1</li>
</ul>
</nav>
</div>
<div id="submenuId2" class="hiddenMenu">
<nav>
<ul>
<li>subMenuItem1</li>
</ul>
</nav>
</div>