views:

16

answers:

3

Hi I am trying to write a script in Jquery that auto selects the current page link in the main nav. The nav is just a simple UL like this:

<ul id="primaryNav">
    <li>
        <a href="retail.html">Home</a>
    </li>
    <li>
        <a href="vision.html">Our Vision</a>
    </li>
    <li>
        <a href="context.html">Town in context</a>
    </li>
</ul>

My Jquery so far looks like this:

$('#primaryNav').find("a[href='"+window.location.href+"']").each(function(){
    $(this).addClass("selected");
}); 

This doesn't work because I think its selecting the whole URL whereas I just need it to select the last part e.g retail.html or vision.html then add the class .selected

Can anyone please help? Thanks in advance.

+2  A: 

Use an attribute ends-with selector to be safe here instead ($= instead of =), like this:

$('#primaryNav').find("a[href$='"+window.location.href.split('/').pop()+"']")
                .addClass("selected");

Also no need for a .each() here, just calling .addClass() will add it to all matched elements (even though there should be one for your example).

Nick Craver
Thanks for the reply Nick. I amended that but can't get it working. An example can be found here http://www.northlight-studios.co.uk/dev/standardLife/retail/retail.html am I missing something obvious here?
mtwallet
@mtwallet - Had a brain lapse there, added the pop version...it's the `location.href` that ends with the anchor's `href`...not the other way around.
Nick Craver
@Nick thanks for your help. Any chance you can run me through how that works and compares to other examples here so I can learn a bit from this?
mtwallet
@mtwallet - Sure, it takes the `location.href`, so the URL in the address bar, splits it into an array on any `/`, so it would he `["http:", "", "www.northlight-studios.co.uk", "dev", "standardLife", "retail", "retail.html"]`, then we're taking the last entry with `.pop()` to get `retail.html`, and we're comparing *that* to the `href` attribute, to make sure it ends with that...and those are the elements that get selected.
Nick Craver
@Nick Awesome! thanks again for the help
mtwallet
A: 

What about a simple substring?

var href = window.location.href;
var htmlPageName = href.substring(href.lastIndexOf('/') + 1);

$('#primaryNav').find("a[href='" + htmlPageName +"']").each(function(){
    $(this).addClass("selected");
}); 
Mark B
@Mark again thanks to you too that works just fine. Any chance you can explain how this works and compares to the @Arnaud F example so I can learn a bit from this?
mtwallet
Sure—`href.lastIndexOf('/') + 1` just gets the index of the last occurrence of `/` (plus one, so we move _past_ the slash). Passing that index as a start index to `href.substring()` returns just the last segment (the page name) from the url string (`href`).
Mark B
+1  A: 

Like that (basing on page name too):

$('#primaryNav').find("a[href$='" + window.location.href.split("/").reverse()[0] + "']").addClass("selected");
Arnaud F.
@Arnaud that works perfectly thanks! Can you explain what you have done there please?
mtwallet
`window.location.href.split("/").reverse()[0]` or `pop()` instead of `.reverse()[0]` splits the current location and the last element of the array is the page name. Once we have the page name, using the selector $= to find the link you want. More details, see jQuery API for the selector $=
Arnaud F.