I am not sure why my navigation doesn't stay highlighted after clicking on it? I reserached and went through similar forums and I couldn't find any related answers. Here is the javascript code I am using.
<style>
.nav.navbar-nav li a.active{
background: red !important;
}
</style>
<script>
$(document).ready(function(){
var activeMenuItem = null;
$("a").click(function(event){
// Remove "active" class from the previous active item
if (activeMenuItem) {
activeMenuItem.removeClass("active");
}
// Add "active" class to the clicked item
$(this).addClass("active");
activeMenuItem = $(this);
// Allow the link to open normally
});
});
</script>
If I change my javascript to prevent default behavior of click, navigation stays highlighted but the links do not open anymore.
<script>
$(document).ready(function(){
var activeMenuItem = null;
$("a").click(function(event){
event.preventDefault(); // **Prevent the default link behavior**
if (activeMenuItem) {
activeMenuItem.removeClass("active");
}
$(this).addClass("active");
activeMenuItem = $(this);
});
});
</script>
Please guide me as I am still learning javascript/jquery. I do not have access to HTML structure to update