I am seeking the ability to implement a dropdown through CSS in various locations within my HTML prototype. This implies that I require the capability to position multiple dropdowns anywhere on the page.
Currently, I utilize this method to generate a single dropdown:
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Click here 01 </button>
<div id="myDropdown" class="dropdown-content">
<a href="#">link</a>
<a href="#">link</a>
<a href="#">link</a>
</div>
</div>
function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}
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');
}
}
} }
How can I create multiple dropdowns using the same JavaScript?
Here is an example that showcases the issue: https://codepen.io/db13/pen/pLGRwr