I am trying to make a navigation menu I did all the HTML and CSS when come to javascript I am struck in the middle I am able to add a class to the
window.myFunction = function(event) {
var elms = document.querySelectorAll('ul li a');
// reset all you menu items
for (var i = 0, len = elms.length; i < len; i++) {
elms[i].classList.remove('active');
}
// mark as active clicked menu item
event.target.classList.add("active");
};
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
header {
width: 100%;
height: auto;
max-width: 600px;
margin: 0 auto;
}
nav {
width: 100%;
height: 40px;
background-color: cornflowerblue;
}
ul {
list-style-type: none;
}
li {
display: inline-block;
}
a {
text-decoration: none;
padding: 8px 15px;
display: block;
text-transform: capitalize;
background-color: pink;
color: #fff;
}
a.active {
background-color: blue;
}
.active {
ackground-color: red;
}