I am trying to make a responsive navbar for my project (I am still a newbie learning this). I want to get a dropdown-like nav anchors when I click on the menu bar (3 horizontally parallel lines). I used the Eventlistener and it is not setting that toggle_button to 'active'. I have been going through YouTube videos, some questions on here, and references all over the internet and I cannot seem to find the solution.
Here is my code block.
const toggleButton = document.getElementsByClassName('toggle_btn')[0];
const navbarLinks = document.getElementsByClassName('navbar_links')[0];
toggleButton.addEventListener('click', () => {
navbarLinks.classList.toggle('active')
});
/*General Styling*/
body {
overflow: hidden;
background-color: grey;
color: #d4af37 ;
font-size: 20px;
display: flex;
justify-content: center;
min-height: 90vh;
background: url("images/background.png") no-repeat;
background-size: cover;
background-position: center;
font-family: Baskerville, "Palatino Linotype", Palatino, "Century Schoolbook L", "Times New Roman", "serif";
}
header{
display: flex;
justify-content: space-between;
align-content: center;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 50px;
padding: 20px 100px;
}
a{
padding-right: 10px;
}
nav ul {
margin: 0;
padding: 0;
display: flex;
}
nav li {
list-style: none;
}
nav li a {
padding: 1rem;
display: block;
}
/*Styling using class*/
.nav {
text-decoration: none;
color: #d4af37;
position: relative;
font-weight: 500;
}
.nav::after{
content: '';
position: absolute;
left: 0;
bottom: -6px;
width: 90%;
height: 1px;
background: #d4af37;
border-radius: 5px;
transform: scaleX(0);
transition: transform .5s;
}
.nav:hover::after{
transform: scaleX(1);
}
/*responsive button*/
.toggle_btn {
position: absolute;
top: 4.5rem;
right: 1rem;
display: none;
flex-direction: column;
justify-content: space-between;
width: 30px;
height: 21px;
margin-right: 200px;
}
.toggle_btn .bar{
height: 3px;
width: 100%;
background-color: #a1852b;
border-radius: 10px;
}
/*responsive nav bar*/
@media only screen and (max-width: 600px) {
.toggle_btn{
display: flex;
}
.navbar_links{
display:none;
}
header{
flex-direction: column;
align-items: flex-start;
}
nav ul{
width: 100%;
flex-direction: column;
}
nav li{
padding-left: 4rem;
}
nav li a{
padding: .5rem 1rem;
}
nav.active {
display: flex;
}
nav.active .nav{
display:block;
width: 100%;
}
}
<!--Logo and Nav-->
<header>
<!--Logo and brandName-->
<div id="logo_div">
<img class="images" id="logo" src="images/logo+name.png" alt="Crux Logo and Brand Name here.">
</div>
<!--Nav bar-->
<div id="nav_div">
<!--Nav-bar toggle-->
<a href="#" class="toggle_btn">
<span class="bar"> </span>
<span class="bar"> </span>
<span class="bar"> </span>
</a>
<nav class="navbar_links">
<ul>
<li><a class="nav" href="#">Home</a></li>
<li><a class="nav" href="#">About Us</a></li>
<li><a class="nav" href="#">Services</a></li>
<li><a class="nav" href="#">Contact</a></li>
</ul>
</nav>
</div>
</header>
Your hamburger menu has a class of toggle_btn
, but your JS is looking for elements with class of toggle_button
, which don't exist, so your event listener is not being attached to anything.