For fixing the toggle button on small screens
the solution:
paste this code instead of your Javascript code and try to read it:
var menu = document.getElementById('menu');
var nav = document.querySelector('nav ul');
var exit = document.getElementById('exit');
menu.addEventListener('click', function(e) {
nav.classList.toggle('hide-mobile');
e.preventDefault();
});
Explain:
FIRST:
If you want to use classlist
method, then you should have a class inside the HTML <nav>
tag.
what you're doing is that you're searching for a class inside nothing, because what getElementById('')
does is that it searches inside the DOM (the HTML file) for an Id with the name that matches the passed argument; mean in your case:
getElementById('nav')
: it's searching for an #Id with the name of nav but you don't have.
So you have to decide:
you may use getElementById('')
but you need to add an id with the name of nav to your <nav>
tag; so <nav id="nav">
.
or you may use document.querySelector('nav')
instead of document.getElementById('nav')
.
SECOND
after fixing the first problem you'll see another one and it's that your toggle button on small screens is not working.
That's because you're toggling the "hide-mobile" class on the <nav>
and you don't want that, you want to toggle that class on the <ul>
element because it's the element that contains the "hide-mobile" class by default.