I have started learning web programming as a project and I've been having a hard time with getting my links to show as active on the navbar. I did start by looking for similar questions asked in the past but none of the answers seemed to fix my problem.
Here is my code
<div>
<hr>
<nav class="container-fluid navbar navbar-expand-sm bg-dark navbar-dark" style="padding-left: 75px; margin-top: -16px;">
<ul class="navbar-nav">
<li class="active nav-item">
<a class="nav-link active" style = "padding-left: 0px; color: white;" href="#">Most Popular</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#" style="color: white">News</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#" style="color: white">Sports</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#" style="color: white">Science</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#" style="color: white">Politics</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#" style="color: white">Economics</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#" style="color: white">Random</a>
</li>
<li class="nav-item">
<a class="nav-link " style="padding-left: 480px; color: white; " href="#">Log in</a>
</li>
</ul>
</nav>
</div>
and I tried using this javascript I found but it hasn't worked so far
$('.nav li').click(function(){
$('.nav li').removeClass('active');
$(this).addClass('active');
})
Hopefully, my code isn't too funky since I am just getting started. Any help would be tremendously appreciated thank you very much!
There are multiple issues...
.nav
class used anywhere in the markup. It should be $('.navbar-nav .nav-link').style="color:white"
on the links which will override any changes you make with the active
class.nav-link
instead of the li,.navbar-dark .nav-item > .nav-link.active {
color:white;
}
$('.navbar-nav .nav-link').click(function(){
$('.navbar-nav .nav-link').removeClass('active');
$(this).addClass('active');
})