I am working on a navigation for a site and need some guidance on dynamically adding a class to the active link. In addition, once that link is established and I need to reference back to the parent and have it "show".
This is what I am working with. The navigation is accordion style but does not use the Accordion UI.
<ul id="menu3">
<li><a href="{site_url}">Home</a></li>
<li><a class="drop" href="#">Information</a>
<ul>
<li><a href="{site_url}information/audio">Audio</a></li>
<li><a href="{site_url}information/video">Video</a></li>
<li><a href="{site_url}information/presentations">Presentations</a></li>
</ul>
</li>
<li><a class="drop" href="#">Clinics</a>
<ul>
<li><a href="{site_url}clinics/one">Office 1</a></li>
<li><a href="{site_url}clinics/two">Office 2</a></li>
</ul>
</li>
<li><a href="{site_url}forms/">Forms</a></li>
<li><a href="{site_url}success_stories/index">Success Stories</a></li>
Then I have a bit of jQuery to initially hide the submenu items:
$(function(){
$('ul#menu3 ul').hide();
$('ul#menu3 > li > a.drop').click(function(){
$(this).parent().children('ul').toggle("slow");
return false;
});
So far so good. Everything works.
Now I would like to dynamically highlight the active link and I tried using:
var path = location.pathname.substring(1);
if ( path )
$('ul#menu3 a[href$="' + path + '"]').attr('class', 'selected');
but it doesn't seem to be enough to add the correct class.
The last thing I need to do is to have the navigation open to the active group. For example if Audio is the current page, the Information section of the navigation accordion would be open to show the active link.
I would really appreciate some help on this. Thanks.