Drop down window.onclick javascript is only working for one menu

This menu works if there is only one. But if I wanted to do 2 of them, the window.onclick = function(event) only works for one menu. Why is that and is it easily fixable? How could I make the 2 menus work the same?.. javascript only please.. No jquery.

function openDrop() {
    document.getElementById("myDropdown").classList.toggle("show");
}

// Close the dropdown if the user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {

    var dropdowns = document.getElementsByClassName("dropdown-content");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}
.dropbtn {
    background-color: #6dc14b;
    color: white;
    padding: 16px;
    font-size: 16px;
    border: none;
    cursor: pointer;
    width:100%;
}

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

.dropdown {
    width:300px;
    margin:0 auto;
}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #ddd;
    min-width: 300px;
    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: #b9d8eb;}

.show {display: block;}

}
<div class="dropdown">
<button onclick="openDrop()" class="dropbtn">Company A</button>
  <div id="myDropdown" class="dropdown-content">
    <a>Type 1</a>
    <a href="#contact"> Type 2</a>
  </div>
</div>

Upvotes: 1

Views: 938

Answers (1)

Maarti
Maarti

Reputation: 3719

You need to pass the id of your dropdown as a parameter of your openDrop() function, here is a quick fix:

function openDrop(menu) {
    document.getElementById(menu).classList.toggle("show");
}

// Close the dropdown if the user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {

    var dropdowns = document.getElementsByClassName("dropdown-content");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}
.dropbtn {
    background-color: #6dc14b;
    color: white;
    padding: 16px;
    font-size: 16px;
    border: none;
    cursor: pointer;
    width:100%;
}

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

.dropdown {
    width:300px;
}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #ddd;
    min-width: 300px;
    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: #b9d8eb;}

.show {display: block;}

}
<div class="dropdown">
<button onclick="openDrop('myDropdown1')" class="dropbtn">Company A</button>
  <div id="myDropdown1" class="dropdown-content">
    <a>Type 1</a>
    <a href="#contact"> Type 2</a>
  </div>
</div>

<div class="dropdown" style="float:right">
<button onclick="openDrop('myDropdown2')" class="dropbtn">Company B</button>
  <div id="myDropdown2" class="dropdown-content">
    <a>Type 3</a>
    <a href="#contact"> Type 4</a>
  </div>
</div>

Upvotes: 1

Related Questions