Buttons suddenly not working and dropdown list is not functioning after some styling. When the dropdown was working it was opening to the top right side no matter which button you pressed. I tried a few things to fix it but I couldn't find an answer. I've stared at the code for a while and I really can't see what I did wrong. I'm assuming I've just been looking at it to long. At this point I'm considering restarting the button portion of the site so I hopefully don't make the same mistake twice. This is sized for an Iphone SE in the dev debugger tool. I'm sure one of you will spot the issue right away so any help would be greatly appreciated.
HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="en">
<title>Spellmann Laboratories</title>
<link rel="stylesheet" href="style.css" type="text/css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css">
</head>
<body>
<header>
<h1>Spellmann Laboratories</h1>
<nav>
<ul class="navbar">
<li class="navitem"><a href="#">Home</a></li>
<li class="navitem"><a href="../Spellmann-Laboratories/about.html">About</a></li>
<li class="navitem"><a href="../Spellmann-Laboratories/Contact.html">Contact</a></li>
</ul>
</nav>
</header>
<div class="parent">
<div class="parentcontainer">
<button onclick="myfunction()" class="dropbtn">Cannabinoids</button>
<i class="fa fa-angle-right" aria-hidden="true"></i>
<div id="mydropdown" class="dropdown">
<a>Link1</a>
<a>Link2</a>
<a>Link3</a>
</div>
</div>
<div class="parentcontainer">
<button onclick="myfunction()" class="dropbtn">Cathinones</button>
<i class="fa fa-angle-right" aria-hidden="true"></i>
<div id="mydropdown" class="dropdown">
<a>Link1</a>
<a>Link2</a>
<a>Link3</a>
</div>
</div>
<div class="parentcontainer">
<button onclick="myfunction()" class="dropbtn">Dissociatives</button>
<i class="fa fa-angle-right" aria-hidden="true"></i>
<div id="mydropdown" class="dropdown">
<a>Link1</a>
<a>Link2</a>
<a>Link3</a>
</div>
</div>
<div class="parentcontainer">
<button onclick="myfunction()" class="dropbtn">Opioids</button>
<i class="fa fa-angle-right" aria-hidden="true"></i>
<div id="mydropdown" class="dropdown">
<a>Link1</a>
<a>Link2</a>
<a>Link3</a>
</div>
</div>
<div class="parentcontainer">
<button onclick="myfunction()" class="dropbtn">Phenethylamines</button>
<i class="fa fa-angle-right" aria-hidden="true"></i>
<div id="mydropdown" class="dropdown">
<a>Link1</a>
<a>Link2</a>
<a>Link3</a>
</div>
</div>
<div class="parentcontainer">
<button onclick="myfunction()" class="dropbtn">tryptamines</button>
<i class="fa fa-angle-right" aria-hidden="true"></i>
<div id="mydropdown" class="dropdown">
<a>Link1</a>
<a>Link2</a>
<a>Link3</a>
</div>
</div>
</div>
<div class="parentimg">
<img class="placeholderimgcontent" src="../XSS4-Repository/Images/placeholderimg.jpeg">
</div>
<div class="insta">
<a href="#">Follow us on Instagram!</a>
</div>
<script defer type="text/javascript" src="../Spellmann-Laboratories/main.js"></script>
</body>
</html>
CSS:
body {
font-family: Georgia, 'Times New Roman', Times, serif;
}
h1 {
text-align: center;
transform: translate(0,140px);
font-size: 3.5em;
}
.navbar {
background-color: #666;
display: flex;
list-style: none;
justify-content: space-evenly;
flex-wrap: wrap;
padding-top: 50px;
padding-bottom: 100px;
margin: 25px 25px 0 25px;
}
.spellab {
font-weight: bold;
font-size: 3.5em;
margin-top: 50px;
}
.navitem {
margin-top: 60px;
font-size: 2.25em;
transform: translate(-22px,50px);
}
.navitem a {
color: black;
text-decoration: none;
text-align: center;
}
.parent {
background-color: #666;
margin: 25px 150px 0 150px;
padding: 15px 0 15px 0;
font-size: 2em;
}
.parentcontainer {
text-align: center;
margin: auto;
}
button {
border: none;
background-color: #666;
font-size: 1.25em;
}
.dropdown a {
display: none;
}
.show {
display: block;
}
.parentimg {
display: flex;
justify-content: center;
}
.placeholderimgcontent {
height: auto;
width: auto;
margin: 25px 25px 0 25px;
}
.insta {
text-align: center;
margin: 25px 25px 0 25px;
}
.insta a {
display: flex;
justify-content: center;
font-size: 3em;
text-decoration: none;
color: black;
background-color: #666;
padding-top: 50px;
padding-bottom: 50px;
}
a:hover, a:focus {
background-color: #999;
cursor: pointer;
}
JS:
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
document.querySelector('.dropbtn').addEventListener('click', function() {
document.getElementById("mydropdown").classList.toggle("show");
});
// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
if (!event.target.matches('.dropbtn')) {
var dropdowns = document.getElementsByClassName("dropdown");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}