1

I'm trying to follow this example.

I've loaded this script:

<script>

// current page highlight
    $(document).ready(function() {
    $("[href]").each(function() {
        if (this.href == window.location.href) {
            $(this).addClass("active");
            }
        });
    });

</script>

Into the <head> section for these files: index.html (the home page), about.html, and store.html

For this site. Services won't ever need to be highlighted, and Blog and My Account are currently dead links.

Then I added the corresponding class to my CSS:

.active {
color:#337ab7;
}

So when we're on Home (index.html), the Home link should be #337ab7, when we're on About (about.html), the About link should be #337ab7, and when we're on Store (store.html), the store link should be #337ab7.

But right now, still no result. What do I need to change about the JavaScript, CSS, or HTML to make this function apply?

Here's the HTML for the Nav Menu in question:

EDIT: Added active class to the links in question:

<nav class="navbar navbar-default">
<div class="container">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
        <button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
                data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
        </button>
        <div>
            <a class="navbar-brand" href="http://nowordpress.gatewaywebdesign.com/">
            <img src="assets/images/gatewaylogo.png">
            </a>
        </div>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
        <ul class="nav navbar-nav navbar-right">
            <li><a href="http://nowordpress.gatewaywebdesign.com/index.html" class="active">Home <span
                    class="sr-only">(current)</span></a></li>
            <li><a href="http://nowordpress.gatewaywebdesign.com/about.html" class="active">About</a></li>
            <li class="dropdown">
                <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
                   aria-expanded="false">
                    Services
                    <span class="caret"></span>
                </a>
                <ul class="dropdown-menu">
                    <li><a href="http://nowordpress.gatewaywebdesign.com/website-design.html">Website Design</a></li>
                    <li><a href="http://nowordpress.gatewaywebdesign.com/graphic-design.html">Graphic Design</a></li>
                    <li><a href="http://nowordpress.gatewaywebdesign.com/promotional-products.html">Promotional Products</a></li>
                    <li><a href="http://nowordpress.gatewaywebdesign.com/search-engine-marketing.html">Search Engine Marketing</a></li>
                    <li><a href="http://nowordpress.gatewaywebdesign.com/wordpress-conversion.html">WordPress Conversion</a></li>
                </ul>
            </li>
            <li><a href="/store.html" class="active">Store</a></li>
            <li><a href="#">Blog</a></li>
            <li><a href="#">My Account</a></li>
        </ul>

        </div><!-- /.navbar-collapse -->
    </div><!-- /.container-fluid -->
</nav> 

And again here is the link for the live site. Thank you.

Chameleon
  • 99
  • 1
  • 9
  • 1
    `$(this).addClass("active");` you have no `.active` class in CSS - did you mean `$(this).addClass("current-link");` – Jaromanda X May 24 '17 at 14:50
  • 1
    You could use CSS `:target` selector? – evolutionxbox May 24 '17 at 14:50
  • if the url of that link is exactly the same as the one you are currently visiting you should go with `:target` and only in the edge case that they differ use something like .active as pseudo target – GottZ May 24 '17 at 14:51
  • @JaromandaX sorry I changed "current-link" to "active" in the CSS above. It's `.active` in my code. – Chameleon May 24 '17 at 14:52
  • 1
    Check your console. For some reason, you're not applying class `active` to any alement. – apires May 24 '17 at 14:52
  • I don't see why you need any JavaScript. You're using static pages so just add the class to the current link. – Darkrum May 24 '17 at 14:53
  • @doutriforce yeah it's giving me this error: http://i.imgur.com/C3d1KME.jpg How do I add the `active` to an element? You mean I should do this? `
  • Store
  • ` Tried that and still no result. – Chameleon May 24 '17 at 14:55
  • @Darkrum but the current link is going to change based on what page we've navigated to, right? – Chameleon May 24 '17 at 14:56
  • @Chameleon, that's right. Fix it before going on. – apires May 24 '17 at 14:56
  • @Chameleon It doesn't matter your site is static just add the active class to the link. So if your in your about page just add active to that links classes in the html. – Darkrum May 24 '17 at 14:59
  • @Darkrum I've added the `active` class to the links in question - see above edit - index.html, about.html, and store.html - still no result though. – Chameleon May 24 '17 at 15:04
  • @Chameleon, change `.active` to `.navbar-default .navbar-nav > li > a.active` in your stylesheet. http://i.imgur.com/rp4bYSS.png – apires May 24 '17 at 15:16
  • @doutriforce I tried it, no result (stylesheet line 84) – Chameleon May 24 '17 at 15:21
  • @Chameleon, dude, you put 2 dots on class name. http://i.imgur.com/c6DS3Oc.png – apires May 24 '17 at 15:25
  • @doutriforce Oh my God - that's fixed now. Now if you refresh the page you'll be able to see the problem - all of the links that are given that class are highlighted. – Chameleon May 24 '17 at 15:29
  • @Chameleon, okay, so only the current link should have `active` class. – apires May 24 '17 at 15:31