views:

26

answers:

1

Hi

I need to highlight the current page in my left nav.

The nav has to be loaded externally via an .shtml include:

<!--#include file="leftnav-menu.inc"-->

My urls take the form of:

www.xxx.com/mission-critical.shtml

but sometimes just:

www.xxx.com/energy.shtml (eg one word no hyphen)

My nav lists it as 'Mission critical'

How can I highlight the ul li with "class=selected"? I've seen something like this:

$(function(){
   var path = location.pathname.substring(1);
   if ( path )
     $('.leftmenuNav ul li a[@href$="' + path + '"]').attr('class', 'selected');
 });    

Can't quite get my head around the string splitting etc...

Sample nav bar:

<ul>
<li><a href="corporate-responsibility.shtml">Corporate responsibility</a></li>
<li><a href="overview.shtml">Overview</a></li>
<li><a href="governance.shtml">Governance</a></li>
<li><a href="our-approach.shtml">Our approach</a></li>
</ul>
A: 

Okay, the jQuery syntax was slightly wrong. This should work:

$.ready(function()
{
   var path = location.pathname.substring(location.pathname.lastIndexOf("/") + 1);
   if ( path )
     $('.leftmenuNav ul li a[href="' + path + '"]').attr('class', 'selected');
});

Also, make sure the leftmenuNav is right (your code above doesn't show it)

Matthew Flaschen
Genius. That worked a treat. Many thanks for your help!
Don West