So i've been on the internet for hours looking for a solution for this, i've tried at least 20 different things and none worked. I need to know how to make my Hamburger menu close when a link inside of the menu is clicked.
My HTML:
<!--
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<div class="topnav" id="myTopnav" style="position:absolute;top:0px;">
<a href="#" class="active" class="nav-item" id="fn">#</a>
<a href="#" class="nav-item">#</a>
<a href="#" class="nav-item">#</a>
<a href="#" class="nav-item">#</a>
<a href="#" class="nav-item">#</a>
<a href="#" class="nav-item">#</a>
<a href="#" class="nav-item">#</a>
<a href="#" class="nav-item">#</a>
<a href="javascript:void(0);" class="icon" onclick="myFunction()">
</a>
</div>
My CSS:
.topnav .icon, #myTopnav, .active {
visibility: visible;
z-index: 10000;
}
.topnav {
width: 100%;
}
.nav, .top-heading {
display: none;
}
#fn {
color: #ffffff;
font-size: 32px;
}
.topnav {
background: linear-gradient(to top, #ff6699 0%, #ff9999 100%);
overflow: hidden;
}
.topnav a {
float: left;
display: block;
color: #ffffff;
text-align: center;
padding: 26px 16px;
text-decoration: none;
font-family: 'Alegreya Sans SC', sans-serif;
font-size: 28px;
}
.topnav a:hover {
background-color: #ffffff;
color: black;
}
.active {
background: linear-gradient(to top, #ff6699 0%, #ff9999 100%);
color: #ffffff;
}
.topnav a:not(:first-child) {display: none;}
.topnav a.icon {
float: right;
display: block;
}
.topnav.responsive {position: relative;}
.topnav.responsive a.icon {
position: absolute;
right: 0%;
top: 0;
}
.topnav.responsive a {
float: none;
display: block;
text-align: left;
}
}
My Javascript (this here is what allows the menu to open, if there is a way for me to keep this functional and also be able to close it when link is clicked would be great):
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
What I have tried:
I've tried different Javascript code, none seemed to work with the code i already have.