0

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>
Barmar
  • 741,623
  • 53
  • 500
  • 612

1 Answers1

0

$(menuId).is(":visible") is never returning true. The next time you click on the link, the $(document).mouseup() handler runs before the onclick handler runs. Since you're not clicking on the DIV, it hides the DIV. Then the onclick handler runs, it sees that the DIV is hidden, so it shows it again.

When detecting a click outside the DIV, you also need to exclude clicks on the menu titles.

You shouldn't add mouseup handler every time you call hideMenu(), as you create multiple handlers that all run the next time you click. Declare that event handler just once, and have it simply close the :visible menu.

function hideMenu(menuId) {
  if ($(menuId).is(":visible")) {
    $(menuId).hide();
  } else {
    $(".hiddenMenu:visible").hide();
    $(menuId).show('800');
  }

  console.log(menuId);
}

$(document).mouseup(function(e) {
  var div = $(".hiddenMenu:visible");
  if (!div.is(e.target) && div.has(e.target).length === 0 && !$(e.target).hasClass("menutitle")) {
    div.hide();
  }
});
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 class="menutitle" onclick="hideMenu('#submenuId1')">Link</a></li>
      <li><a class="menutitle" 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>
Barmar
  • 741,623
  • 53
  • 500
  • 612