This is my first time writing a JS function. I am trying to get the navigation bar to open when the hamburger is clicked but it's not working. I have attached my HTML and JS. It works when I don't allow the function to have any parameter and instead manually write "nav" where nav_type is, so I'm not sure why it's not working.
The code:
/* Toggle between showing and hiding the navigation menu links when the user clicks on the hamburger menu / bar icon */
function myFunction(nav_type) {
var i;
var x;
for (i = 1; i < 6; i++) {
x = document.getElementById(nav_type + i);
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
}
<nav aria-label="Main Navigation" class="topnav">
<a aria-hidden="true" href="javascript:void(0);" ; class="menu show-mobile" onclick="myFunction(" nav ")">
<img src="img/menu-icon.svg" alt="toggle menu" />
</a>
<ul class="topnav">
<li><a href="index.html" class="show-mobile">Home</a></li>
<li><a href="humans/humans.html" id="nav1">Humans</a></li>
<li><a href="other/other.html" id="nav2">Plants, Animals, the Universe, and Other
</a></li>
<li class="dropdown" id="nav3">
<button class="dropbtn" id="nav4" onclick="myFunction(" drp ")">More
<img src="img/down-arrow.svg" alt="dropdown arrow" class="dropdown-icon icon"/>
</button>
<ul class="topnav dropdown-content">
<li><a href="presentations.html" id="drp1">Presentations</a></li>
<li><a href="about-islam.html" id="drp2">About Islam</a></li>
<li><a href="contact-us.html" id="drp3" class="active">Contact Us</a></li>
</ul>
</li>
</ul>
</nav>