Search code examples
javascriptjqueryhtmlcssmenubar

How to hide the other dropdown when others is selected


I have a menubar with 2 options which shows dropdowns on click, technically theyre all shown even you click only one. All I wanted to do is to hide other dropdowns after when I click other menu items.

What is wrong with my code?

BODY:

<body>
    <ul>
  <li class="File">
    <a href="javascript:void(0)" class="dropbtn" onclick="myFunction()">File</a>
    <div class="dropdown-content" id="myDropdown">
      <a href="#">Open</a>
      <a href="#">Save</a>
      <a href="#">Save As</a>
      <a href="#">Close</a>
    </div>
    <li class="Edit">
    <a href="javascript:void(0)" class="dropbtn" onclick="myFunction()">Edit</a>
    <div class="dropdown-content" id="myDropdown2">
      <a href="#">Undo</a>
      <a href="#">Redo</a>
      <a href="#">Cut</a>
      <a href="#">Copy</a>
      <a href="#">Paste</a>
    </div>
  </li>
</ul>
<div id="TITLE" style="font-family: verdana; font-size: 36px; margin-left: 1200px; margin-top: -45px; color: white;">
    PANTS
</div>
<!-- THE FUNCTION FOR CLICK -->
<script>
var drptochoose;
function myFunction() {
    document.getElementById("myDropdown").classList.toggle("show");
    document.getElementById("myDropdown2").classList.toggle("show");
}

window.onclick = function(e) {
  if (!e.target.matches('.dropbtn')) {

    var dropdowns = document.getElementsByClassName("dropdown-content");
    for (var d = 0; d < dropdowns.length; d++) {
      var openDropdown = dropdowns[d];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}
</script>
<!-- END OF FUNCTION FOR CLICK -->
    </body>

CSS:

<style>
ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    background-color: green;
    font-family: verdana;
    font-size: 14px;
}

li {
    float: left;
}

li a, .dropbtn {
    display: inline-block;
    color: white;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
}

li a:hover, .dropdown:hover .dropbtn {
    background-color: red;
}

li.dropdown {
    display: inline-block;
}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

.dropdown-content a {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
    text-align: left;
}

.dropdown-content a:hover {background-color: #f1f1f1}

.show {display:block;}
<script src="jquery.js"></script>
        </style>

Solution

  • The problem with your code is that it toggles both dropdowns. Therefore, when they are both not shown they get this class.

    You can do something like this:

    <body>
      <ul>
        <li class="File">
          <a href="javascript:void(0)" class="dropbtn" onclick="myFunction(myDropdown)">File</a>
          <div class="dropdown-content" id="myDropdown">
            <a href="#">Open</a>
            <a href="#">Save</a>
            <a href="#">Save As</a>
            <a href="#">Close</a>
          </div>
        </li>
        <li class="Edit">
          <a href="javascript:void(0)" class="dropbtn" onclick="myFunction(myDropdown2)">Edit</a>
          <div class="dropdown-content" id="myDropdown2">
            <a href="#">Undo</a>
            <a href="#">Redo</a>
            <a href="#">Cut</a>
            <a href="#">Copy</a>
            <a href="#">Paste</a>
          </div>
        </li>
      </ul>
      <div id="TITLE" style="font-family: verdana; font-size: 36px; margin-left: 1200px; margin-top: -45px; color: white;">
        PANTS
      </div>
      <!-- THE FUNCTION FOR CLICK -->
    
      <script>
        var drptochoose;
    
        function hideDropdowns(excludeId) {
          var dropdowns = document.getElementsByClassName("dropdown-content");
          for (var d = 0; d < dropdowns.length; d++) {
            var openDropdown = dropdowns[d];
            if (openDropdown.classList.contains('show') && excludeId !== openDropdown.id) {
              openDropdown.classList.remove('show');
            }
          }
        }
    
        function myFunction(id) {
          id.classList.toggle("show");
          hideDropdowns(id.id);
        }
    
        window.onclick = function(e) {
          if (!e.target.matches('.dropbtn')) {
            hideDropdowns();
          }
        }
      </script>
    
      <!-- END OF FUNCTION FOR CLICK -->
    </body>