1

I am quite desperate, I checked every similar questions on stackoverflow and tried many solutions found on google but in vain... I'm really unable to find a working solution to my problem.
I am developping a site with Bootstrap 4 and I have a navbar. When I click on one of the nav-link I want it to have the "active" class, while others stay normal. I might not select the right elements or use wrong jquery/javascript code.

The navbar html code (1) is in a separate header.html file that I load with JS at the beginning of the html page (2):
(1)

<nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top ml-auto">
        <div class="container">
            <a class="navbar-brand" href="../index.html#"><img src="../images/DSIMB.svg" alt=""></a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarResponsive">
                <ul class="navbar-nav ml-auto">
                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle" href="" id="dropdown01" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Team</a>
                        <div class="dropdown-menu" aria-labelledby="dropdown01">
                            <a class="dropdown-item" href="members.html#">Members</a>
                            <a class="dropdown-item" href="publications.html#">Publications</a>
                            <a class="dropdown-item" href="teaching.html#">Teaching</a>
                        </div>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="web_tools.html#">Web Tools</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="research.html#">Research</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="infrastructures.html#">Infrastructures</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="news.html#">News</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="contacts.html#">Contacts</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

(2)

<script>
        $(function() {
            $("#header").load("./header.html");
        });
</script>

Here is the JS I put at the end of the html page just before the </body> tag:

<script>
    $('.navbar .nav-item .navlink').click(function() {
        $('.navbar .nav-item').removeClass('active');
        $(this).addClass('active');
    })
</script>

And here the active css class I want to set to the active nav-link:

.navbar .nav-item > .nav-link.active  {
    color:white;
    font-weight: 700;
}

Please tell me what I am doing wrong...


UPDATE

I put here the solution I found thanks to a simple snippet (that I adapted to my code) taken from this site which @NmiDev kindly pointed to me.
I add my custom active class to the link in the navbar targeted thanks to href / location parsing:

$("#header").load("./header.html", function(){
    $('a[href="' + location.pathname.split("/")[2] + '"]').addClass("active-nav");
});
Gabriel C
  • 141
  • 1
  • 10

1 Answers1

3

Here a snippet.

Hope it's help you.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <style>
        .active  {
            color: red !important;
        }
    </style>
</head>
<body>
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top ml-auto">
        <div class="container">
            <a class="navbar-brand" href="../index.html#"><img src="../images/DSIMB.svg" alt=""></a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarResponsive">
                <ul class="navbar-nav ml-auto">
                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle" href="" id="dropdown01" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Team</a>
                        <div class="dropdown-menu" aria-labelledby="dropdown01">
                            <a class="dropdown-item" href="#">Members</a>
                            <a class="dropdown-item" href="#">Publications</a>
                            <a class="dropdown-item" href="#">Teaching</a>
                        </div>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#">Web Tools</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#">Research</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#">Infrastructures</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#">News</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#">Contacts</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

    <script>
        $('.nav-link').click(function() {
            $('.nav-link').removeClass('active');
            $(this).addClass('active');
        })
    </script>
</body>
</html>

It's seems that you don't need to override the css active class. White is the default behavior.

NmiDev
  • 76
  • 4
  • Thank you for trying to help, but it is not working... with only `href="#"` maybe but I have real link instead and the active class is not persisting. I also tried like this with the default bootstrap snippet on their website and it works as long as it stays with `href="#"`, but then if I put real hrefs when the new page loads the active class disappears – Gabriel C Apr 05 '20 at 14:24
  • This probably means that you reload your entire page, including your header. This would explain the non-persistence of the active class. Make sure to load the content of your route under your header ? – NmiDev Apr 05 '20 at 14:32
  • 1
    I don't have the full context of the project but this can help you perhaps : https://css-tricks.com/snippets/jquery/add-active-navigation-class-based-on-url/ – NmiDev Apr 05 '20 at 14:36
  • Oh ok I see, maybe that is the problem, I followed [this solution](https://stackoverflow.com/a/18712605/6401758) to include my headers in my static html pages. I am not very experienced so decided to go for a full static html css and js website with bootstrap 4 as framework (no php etc). Can you suggest a way to not reload the entire page please ? – Gabriel C Apr 05 '20 at 14:59
  • You're want to create a single page application. Modern frameworks use this concept (React, Angular or Vue). You want to make a client-side routing. I'm not sure there is a simple way to do that. In your entry point, index.html, make your header and footer static an you can handle the click on each link and display the content in a root DIV. Here a old ressource but it can help to understand what your are trying to do : https://joakim.beng.se/blog/posts/a-javascript-router-in-20-lines.html – NmiDev Apr 05 '20 at 15:17
  • Another example with jquery if you prefer : https://medium.com/@arkaindas/single-page-apps-with-jquery-routing-5ebd8fda6bae – NmiDev Apr 05 '20 at 15:20
  • Ok solved ! I chose to use the solution you pointed at first to css-trick link, tweaked it a bit and now it works: `$("#header").load("./header.html", function(){ $('a[href="' + location.pathname.split("/")[2] + '"]').addClass("active-nav"); });` thank you for your kind help ! – Gabriel C Apr 05 '20 at 16:38