I am working on site navigation and need a guide to dynamically add a class to an active link. Also, as soon as this link is set, and I need to refer to the parent and show it.
This is what I work with. Navigation is an accordion style but does not use the Accordion interface.
<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 little 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 is working.
Now I would like to dynamically highlight the active link, and I tried to use:
var path = location.pathname.substring(1);
if ( path )
$('ul#menu3 a[href$="' + path + '"]').attr('class', 'selected');
but that doesn't seem to be enough to add the right class.
, , - . , - , .
. .