Search code examples
javascripthtmldrop-down-menudropdown

handle mutliple dropdown menus


I have multiple buttons where a menu drops down when you click on one button AND all the other dropdown menus will be closed. I currently have 9 of those and corresponding 9 functions for that purpose:

function myFunction5() {
    document.getElementById("myDropdown1").classList.remove("show");
    document.getElementById("myDropdown2").classList.remove("show");
    document.getElementById("myDropdown3").classList.remove("show");
    document.getElementById("myDropdown4").classList.remove("show");
    document.getElementById("myDropdown5").classList.toggle("show");
    document.getElementById("myDropdown6").classList.remove("show");
    document.getElementById("myDropdown7").classList.remove("show");
    document.getElementById("myDropdown9").classList.remove("show"); }

This opens dropdown menu 5 and closes all others.

I would like to have only one function for that instead of 9. How do i do that?

EDIT:

Complete code example:

<!DOCTYPE html>
<html>
<head>
<style>
.dropbtn {
  background-color: #3498DB;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}

.dropbtn:hover, .dropbtn:focus {
  background-color: #2980B9;
}

.dropdown {
  position: relative;
  display: inline-block;
}

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

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

.dropdown a:hover {background-color: #ddd;}

.show {display: block;}
</style>
</head>
<body>

<div class="dropdown">
  <button onclick="myFunction1()" class="dropbtn">Dropdown</button>
  <div id="myDropdown1" class="dropdown-content">
    <a href="#home">Home</a>
    <a href="#about">About</a>
    <a href="#contact">Contact</a>
  </div>
</div>

<div class="dropdown">
  <button onclick="myFunction2()" class="dropbtn">Dropdown</button>
  <div id="myDropdown2" class="dropdown-content">
    <a href="#home">Home</a>
    <a href="#about">About</a>
    <a href="#contact">Contact</a>
  </div>
</div>

<script>
function myFunction1() {
  document.getElementById("myDropdown1").classList.toggle("show");
  document.getElementById("myDropdown2").classList.remove("show");
}

function myFunction2() {
  document.getElementById("myDropdown1").classList.remove("show");
  document.getElementById("myDropdown2").classList.toggle("show");
}

</script>
</body>
</html>

Imagine this 9 times...


Solution

  • Try this:-

    <script type="text/javascript">
        function manageDropdown(id)
        {  
            var dropdown = document.getElementsByClassName('dropdown-content');
            for(i=0;i<dropdown.length;i++)
            {
                 var innerId = dropdown[i].getAttribute('id');
                 if (innerId == id) { 
                    dropdown[i].classList.remove("hide");
                 } else {
                    dropdown[i].classList.add("hide");
                 }
            } 
        }
    </script>
    
    <div class="dropdown">
      <button onclick="manageDropdown('myDropdown1')" class="dropbtn">Dropdown</button>
      <div id="myDropdown1" class="dropdown-content">
        <a href="#home">Home</a>
        <a href="#about">About</a>
        <a href="#contact">Contact</a>
      </div>
    </div>
    
    <div class="dropdown">
      <button onclick="manageDropdown('myDropdown2')" class="dropbtn">Dropdown</button>
      <div id="myDropdown2" class="dropdown-content hide">
        <a href="#home">Home</a>
        <a href="#about">About</a>
        <a href="#contact">Contact</a>
      </div>
    </div>