Skip to content Skip to sidebar Skip to footer

About Clickable Dropdown Menu

I follow this link to try to make a clickable dropdown menu. I notice the code from the link is for one dropdown menu and I think this code is for create the dropdown menu.

Solution 1:

You are overriding three times window.onclick event function. Only the last function launches. If you combine the three functions in one, it works.

Your code now:

<!DOCTYPE html><html><head><style>/*1st dropdown*/.dropbtn {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}

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

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

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

.dropdown-contenta {
color: black;
padding: 12px16px;
text-decoration: none;
display: block;
}

.dropdowna:hover {background-color: #f1f1f1}

.show {display:block;}


/*2nd dropdown*/.dropbtn2 {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}

.dropbtn2:hover, .dropbtn2:focus {
background-color: #3e8e41;
}

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

.dropdown-content2 {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
overflow: auto;
box-shadow: 0px8px16px0pxrgba(0,0,0,0.2);
z-index: 1;
}

.dropdown-content2a {
color: black;
padding: 12px16px;
text-decoration: none;
display: block;
}

.dropdown2a:hover {background-color: #f1f1f1}

.show2 {display:block;}

/*3rd dropdown*/.dropbtn3 {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}

.dropbtn3:hover, .dropbtn2:focus {
background-color: #3e8e41;
}

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

.dropdown-content3 {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
overflow: auto;
box-shadow: 0px8px16px0pxrgba(0,0,0,0.2);
z-index: 1;
}

.dropdown-content3a {
color: black;
padding: 12px16px;
text-decoration: none;
display: block;
}

.dropdown3a:hover {background-color: #f1f1f1}

.show3 {display:block;}

</style></head><body><h2>Clickable Dropdown</h2><p>Click on the button to open the dropdown menu.</p>

1st dropdown
<divclass="dropdown"><buttononclick="myFunction()"class="dropbtn">Dropdown</button><divid="myDropdown"class="dropdown-content"><ahref="#home">Home</a><ahref="#about">About</a><ahref="#contact">Contact</a></div></div>

2nd dropdown
<divclass="dropdown2"><buttononclick="myFunction2()"class="dropbtn2">Dropdown2</button><divid="myDropdown2"class="dropdown-content2"><ahref="#home">Home2</a><ahref="#about">About2</a><ahref="#contact">Contact2</a></div></div>

3rd dropdown
<divclass="dropdown3"><buttononclick="myFunction3()"class="dropbtn3">Dropdown3</button><divid="myDropdown3"class="dropdown-content3"><ahref="#home">Home3</a><ahref="#about">About3</a><ahref="#contact">Contact3</a></div></div><script>/* When the user clicks on the button, 
toggle between hiding and showing the dropdown content */functionmyFunction() {
    document.getElementById("myDropdown").classList.toggle("show");
}

//for 2nd dropdownfunctionmyFunction2() {
    document.getElementById("myDropdown2").classList.toggle("show2");
}



//for 3rd dropdownfunctionmyFunction3() {
    document.getElementById("myDropdown3").classList.toggle("show3");
}

// Close the dropdown if the user clicks outside of itwindow.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');
        }
    }
  }
  
  if (!event.target.matches('.dropbtn2')) {

    var dropdowns = document.getElementsByClassName("dropdown-content2");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
        var openDropdown = dropdowns[i];
        if (openDropdown.classList.contains('show2')) {
            openDropdown.classList.remove('show2');
        }
    }
  }
  
  if (!event.target.matches('.dropbtn3')) {

    var dropdowns = document.getElementsByClassName("dropdown-content3");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
        var openDropdown = dropdowns[i];
        if (openDropdown.classList.contains('show3')) {
            openDropdown.classList.remove('show3');
        }
    }
  }
}




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

Post a Comment for "About Clickable Dropdown Menu"