In my interface, I have implemented a drop-down menu that consists of two options. When the "Add Equipment" option is clicked, everything functions properly as expected. However, when I select the "Deployed Equipments" option, the drop-down for "Add Equipment" unexpectedly appears.
This is what occurs when I click on "Deployed Equipments":
There seems to be an issue with how the dropdown menus are interacting. Do you see any errors in my implementation?
Below is the CSS code being used:
/* CSS code for dropdown button */
.dropbtn {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
/* Hover and focus styling for dropdown button*/
.dropbtn:hover, .dropbtn:focus {
background-color: #3e8e41;
}
/* Container for dropdown content */
.dropdown {
position: relative;
display: inline-block;
}
/* Styling for dropdown content (initially hidden) */
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
/* Links inside dropdown */
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
/* Change color of links on hover */
.dropdown-content a:hover {background-color: #f1f1f1}
/* Display dropdown menu when active */
.show {display:block;}
Here's the HTML code:
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Add Equipment</button>
<div id="myDropdown" class="dropdown-content">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
</div>
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Deployed Equipments</button>
<div id="myDropdown" class="dropdown-content">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
</div>
And finally, here's the JavaScript code:
<script>
function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}
// Close dropdown menu if user clicks outside
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');
}
}
}
}
</script>