I'd like to be able to place a dropdown via CSS anywhere in my HTML prototype. This means I need to be able to place multiple dropdowns anywhere on the page.
Right now I use this to create 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 do I create multiple dropdowns using the same Javascript?
Here is a demo that does not work: https://codepen.io/db13/pen/pLGRwr