After seeking help for my previous question on Unable to add a background colour to the button in navigation
I have successfully resolved that issue and continued working, but now I am facing a new challenge. I need to change the font color of the navigation bar elements based on which tab is currently active. The CSS appears correct as it styles the "currently active" tab correctly, so it seems like the JavaScript part is where the problem lies.
var header = document.getElementById("navbar");
var btns = header.getElementsByClassName("topnavbar");
for (var i = 0; i < btns.length; i++) {
btns[i].addEventListener("click", function() {
var current = document.getElementsByClassName("active");
current[0].className = current[0].className.replace(" active", "");
this.className += " active";
});
}
.active {
text-decoration: none;
color: #4da6ff;
outline: none;
}
<div id="navbar" >
<ul>
<li> <a class="topnavbar active" href="" id="home"title="Home">HOME </a> </li>
<li> <a class="topnavbar" href="" id="movies" title="Movies">MOVIES </a> </li>
<li> <a class="topnavbar" href="" id="theaters" title="Theaters">THEATERS </a></li>
<li> <a class="topnavbar" href="" id="buytickets" title="Buy Tickets Online">BUY TICKETS ONLINE </a> </li>
<li style="float:right"><a class="btn btn-scope1 navbar-btn" id="btn1" href="">TEST YOUR KNOWLEDGE </a> </li>
</ul>
</div>
What could be the mistake in my approach?