0

I'm using bootstrap v4.3.1 in a ASP.NET MVC core website. I have a navbar where I have the .active class on the a tag on the first li:

<nav class="navbar navbar-expand-md navbar-toggleable-md navbar-dark bg-navbar fixed-top mb-3">
  <div class="container">
    <a
      class="navbar-brand"
      asp-area=""
      asp-controller="Home"
      asp-action="Index"
    >
      <img id="nav-logo" src="~/Images/LOGO's.png" alt="G&Z Installation" />
    </a>
    <button
      class="navbar-toggler"
      type="button"
      data-toggle="collapse"
      data-target=".navbar-collapse"
      aria-controls="navbarSupportedContent"
      aria-expanded="false"
      aria-label="Toggle navigation"
    >
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="navbar-collapse collapse d-md-inline-flex flex-md-row-reverse">
      <ul class="nav ml-auto">
        <li class="nav-item">
          <a
            class="nav-link active text-light nav-text"
            asp-area=""
            asp-controller="Home"
            asp-action="Index"
          >
            Home
          </a>
        </li>
        <li class="nav-item">
          <a
            class="nav-link text-light nav-text"
            asp-area=""
            asp-controller="Home"
            asp-action="SolarPanels"
          >
            Zonnepanelen
          </a>
        </li>
        <li class="nav-item">
          <a
            class="nav-link text-light nav-text"
            asp-area=""
            asp-controller="Home"
            asp-action="AircoAndHeatPump"
          >
            Airco & Warmtepomp
          </a>
        </li>
        <li class="nav-item">
          <a
            class="nav-link text-light nav-text"
            asp-area=""
            asp-controller="Home"
            asp-action="Contact"
          >
            Contact
          </a>
        </li>
        @*
        <li class="nav-item">
          <a
            class="nav-link text-light"
            asp-area=""
            asp-controller="Home"
            asp-action="Privacy"
          >
            Privacy
          </a>
        </li>
        *@
      </ul>
    </div>
  </div>
</nav>;

I added this code:

$(document).ready(function () {
  $("a.nav-link.text-light.nav-text").on("click", function () {
    $(".nav").find(".active").removeClass("active");
    $(this).addClass("active");
  });
});

The active class is not changing after clicking an other li item. It is staying on the first li item. Bootstrap is loaded in the head of the layout page:

<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="~/css/site.css" />

And the scripts are loaded at the bottom of the layout page:

<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<script src="~/js/site.js" asp-append-version="true"></script>

EDIT: When I look at the elements in the browser, I can see the active class been deleted from the first li item when I click the second, but then I see it automatically been added to the first li item again.. As if it refreshes or something.. If I edit the js-code (and the .active class on the li item instead of the a tag), it works, but then the navigation is not working anymore.. This is because of the "e.preventDefault()" where the "e" is for the a tag.. Whitout it will not work for the .active class..

$(document).ready(function () {
$('.nav li a').click(function (e) {

    $('.nav li.active').removeClass('active');

    var $parent = $(this).parent();
    $parent.addClass('active');
    e.preventDefault();
});});

It's been a while since I worked with bootstrap and asp.net mvc. I'm not seeing what I do wrong. Any tips?

2 Answers2

0

Please try this.I take active class in css just for demo purpose you can use your own class.Thanks

$(document).ready(function () {
$("a.nav-link").on("click", function () {
$("a.nav-link").each(function(i,o)
{
$(this).removeClass("active");
});
    $(this).addClass("active");
})
});
.active {
  background-color: #666;
  color: white;
}
<html>
<head>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>

<nav class="navbar navbar-expand-md navbar-toggleable-md navbar-dark bg-navbar fixed-top mb-3">
        <div class="container">
            <a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">
                <img id="nav-logo" src="~/Images/LOGO's.png" alt="G&Z Installation" />
            </a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
                    aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="navbar-collapse collapse d-md-inline-flex flex-md-row-reverse">
                <ul class="nav ml-auto">
                    <li class="nav-item">
                        <a class="nav-link active text-light nav-text" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link text-light nav-text" asp-area="" asp-controller="Home" asp-action="SolarPanels">Zonnepanelen</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link text-light nav-text" asp-area="" asp-controller="Home" asp-action="AircoAndHeatPump">Airco & Warmtepomp</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link text-light nav-text" asp-area="" asp-controller="Home" asp-action="Contact">Contact</a>
                    </li>
                    @*<li class="nav-item">
                            <a class="nav-link text-light" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
                        </li>*@
                </ul>
            </div>
        </div>
    </nav>
    </body>
    </html>
Ricky
  • 488
  • 3
  • 14
  • This is not changing anything in my case.. I think you are using bootstrap v3 instead of v4. I've added a small text above. I can see the active class been removed, but then it automatically comes back to the first li item.. – D. Brugmans Jun 07 '21 at 07:58
0

I found a solution thanks to the answer of Jon here !!! :) I changed the JS code to:

$(document).ready(function() {
  $('li.active').removeClass('active');
  $('a[href="' + location.pathname + '"]').closest('li').addClass('active'); 
});

Now it works fine :)