Is it possible to add an active class to a parent menu when clicking on its sub-menu in Bootstrap? Below is the Bootstrap menu code:
// Active navigation
const currentLocation = location.href;
const menuItem = document.querySelectorAll('.nav-link');
const menuLength = menuItem.length;
for (let i = 0; i < menuLength; i++) {
if (menuItem[i].href === currentLocation) {
menuItem[i].classList.add("active");
}
}
.active {
color: #FF0000;
border-bottom: 2px solid #FF0000;
}
<ul class="navbar-nav m-auto">
<li class="nav-item ">
<a class="nav-link active" href="index.html">Home</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
About
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<a class="dropdown-item " href="about.html">About me</a>
<a class="dropdown-item" href="myportfolio.html">My portfolio</a>
<a class="dropdown-item" href="contact.html">Contact</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link" href="demo.html">Demo</a>
</li>
</ul>
How can I make a parent menu active even after clicking on its sub-menu and being redirected to another page? Any help would be appreciated.