I'm creating drop downs with a code that I found on the web.
Problem is, when I click on one menu that opens, then I click on the next menu, the first menu remains open...
I managed to get just about everything working great on drop downs, links, sizing and colors...
...but it won't close as I click over to the other drop down.
Any and all help would be appreciated!
function myFunction() {
document.getElementById("myDropdown1").classList.toggle("show");
}
window.onclick = function(event) {
if (!event.target.matches('.dropbtn1')) {
var dropdowns = document.getElementsByClassName("dropdown1-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
function FunctionFluids() {
document.getElementById("myDropdown2").classList.toggle("show");
}
window.onclick = function(event) {
if (!event.target.matches('.dropbtn1')) {
var dropdowns = document.getElementsByClassName("dropdown2-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
function FunctionAdditives() {
document.getElementById("myDropdown3").classList.toggle("show");
}
window.onclick = function(event) {
if (!event.target.matches('.dropbtn1')) {
var dropdowns = document.getElementsByClassName("dropdown3-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
function FunctionCleaners() {
document.getElementById("myDropdown4").classList.toggle("show");
}
window.onclick = function(event) {
if (!event.target.matches('.dropbtn1')) {
var dropdowns = document.getElementsByClassName("dropdown4-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
function FunctionWinter() {
document.getElementById("myDropdown5").classList.toggle("show");
}
window.onclick = function(event) {
if (!event.target.matches('.dropbtn1')) {
var dropdowns = document.getElementsByClassName("dropdown5-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
function FunctionLubricants() {
document.getElementById("myDropdown6").classList.toggle("show");
}
window.onclick = function(event) {
if (!event.target.matches('.dropbtn1')) {
var dropdowns = document.getElementsByClassName("dropdown6-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
function FunctionOther() {
document.getElementById("myDropdown7").classList.toggle("show");
}
window.onclick = function(event) {
if (!event.target.matches('.dropbtn1')) {
var dropdowns = document.getElementsByClassName("dropdown7-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
function FunctionNone() {
document.getElementById("myDropdown7").classList.toggle("show");
}
window.onclick = function(event) {
if (!event.target.matches('.dropbtn1')) {
var dropdowns = document.getElementsByClassName("dropdown6-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
.dropbtn1 {
background-color: transparent;
color: white;
padding: 7px 10px 7px 10px;
font-size: 13px;
font-weight: bold;
font-family: "Arial Narrow", Arial, sans-serif;
border: none;
cursor: pointer;
}
.dropbtn1:hover,
.dropbtn1:focus {
background-color: #383838;
}
.dropdown1 {
position: relative;
display: inline-block;
}
.dropdown1-content {
display: none;
position: absolute;
background-color: #eff0f2;
font-size: 13px;
font-weight: bold;
line-height: 2.2;
width: 200px;
padding: 23px 10px 10px 10px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: -501;
border-bottom: 3px solid red;
}
.dropdown1-content a {
color: black;
text-align: left;
font-size: 12px;
padding: 6px 6px 6px 6px;
text-decoration: none;
}
.dropdown1-content a:hover {
background-color: #ddd
}
.show {
display: block;
}
<div style="background-color:black">
<div class="dropdown1">
<button onclick="myFunction()" class="dropbtn1">REFRIGERANTS</button>
<div id="myDropdown1" class="dropdown1-content" style="text-align:left;">
<a href="#">Refrigerants</a><br>
<a href="#">Lubricants</a><br>
<a href="#">Treatments</a><br>
<a href="#">R-134A Products</a><br>
</div>
</div>
<div class="dropdown1">
<button onclick="FunctionFluids()" class="dropbtn1">FLUIDS</button>
<div id="myDropdown2" class="dropdown1-content" style="text-align:left">
<a href="#">Brake Fluid</a><br>
<a href="#">Automatic Transmission Fluid</a><br>
<a href="#">Power Steering Fluid</a><br>
</div>
</div>
<div class="dropdown1">
<button onclick="FunctionAdditives()" class="dropbtn1">ADDITIVES</button>
<div id="myDropdown3" class="dropdown1-content" style="text-align:left">
<a href="#">Fuel Additives</a><br>
<a href="#">Oil Additives</a>
</div>
</div>
<div class="dropdown1">
<button onclick="FunctionCleaners()" class="dropbtn1">CLEANERS</button>
<div id="myDropdown4" class="dropdown1-content" style="text-align:left">
<a href="#">Brake Parts Cleaner</a><br>
<a href="#">Carb & Air Intake Cleaners</a><br>
<a href="#">General Cleaners</a><br>
<a href="#">Battery Cleaners</a><br>
</div>
</div>
</font>
</td>
<td></td>
<td align="left" width="38%">
<font color="#ffffff">
<div class="dropdown1">
<button onclick="FunctionWinter()" class="dropbtn1">WINTER DRIVING</button>
<div id="myDropdown5" class="dropdown1-content" text-align="left">
<a href="#">Link 1</a><br>
<a href="#">Link 2</a><br>
<a href="#">Link 3</a><br>
</div>
</div>
<div class="dropdown1">
<button onclick="FunctionLubricants()" class="dropbtn1">LUBIRICANTS</button>
<div id="myDropdown6" class="dropdown1-content" text-align="left">
<a href="#">Link 1</a><br>
<a href="#">Link 2</a><br>
<a href="#">Link 3</a><br>
</div>
</div>
<div class="dropdown1">
<button onclick="FunctionOther()" class="dropbtn1">OTHER PRODUCTS</button>
<div id="myDropdown7" class="dropdown1-content" text-align="left">
<a href="#">Link 1</a><br>
<a href="#">Link 2</a><br>
<a href="#">Link 3</a><br>
</div>
</div>
</div>