I have looked for answers here, but none fit my specific issue.
I have a Bootstrap 4 navbar that I need to change the active class for the menu item that is clicked. I have the home link active by default. But when I click another link, nothing happens. When I click it again, the active class if found and removed, but the active class is not added to the item I clicked, and the active class goes back to the home link.
The relevant portion of my navbar:
$(document).ready(function() {
$('.nav-link').on('click', function() {
$('.navbar-nav').find('.active').removeClass('active');
$(this).addClass('active');
});
});
.active { color: red }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="navbar-nav">
<li class="nav-item">
<a id="homelink" class="nav-link active" href="index.php">Home<span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a id="aboutlink" class="nav-link" href="about.php">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Portfolio</a>
</li>
</ul>
hrefattribute (index.php, about.php, or #). When it does that, no other code will run. - Heretic Monkey