At the moment, I have a function that highlights my navbar menu items based on which one I clicked.
However, the issue arises when there is a button inside my navbar receiving the highlight
class upon clicking.
I'm unsure of how to modify the JavaScript to ensure that the button does not obtain the highlight
class when clicked.
Below is the current JS implementation:
const menu = document.querySelector('#mobile-menu');
const menuLinks = document.querySelectorAll('.navbar__menu');
const activeMenu = e => {
const elems = document.querySelector('.highlight');
const button = document.querySelector('.button');
if (elems) {
elems.classList.remove('highlight');
}
if(!button.contains(e.target)) {
e.target.className = 'navbar__links highlight';
}
};
menuLinks.forEach(link => {
link.addEventListener('click', activeMenu);
});
Here is the HTML code for the Navbar:
<ul class="navbar__menu">
<li class="navbar__item">
<a href="#home" class="navbar__links" id="homePage">Home</a>
</li>
<li class="navbar__item">
<a href="#about" class="navbar__links" id="about-us">About
Us</a>
</li>
<li class="navbar__item">
<a href="#services" class="navbar__links"
id="service">Services</a>
</li>
<li class="navbar__btn">
<a href="#sign-up" class=" button" id="signup">Sign Up</a>
</li>
</ul>