I have a Nav in which I am trying to use the jQuery addClass method to set the link color of the last link. The problem is that I have to use removeClass for all other links in Nav. this is what i have problems with.
I wrote the code in a naive way, but I know that this is not good programming. Below is the code with ref style sheet.
jQuery('#shop-nav').click(function(){
jQuery("#shop-nav").addClass("clicked");
jQuery("#art-nav").removeClass("clicked");
jQuery("#obj-nav").removeClass("clicked");
jQuery("#acc-nav").removeClass("clicked");
});
jQuery('#art-nav').click(function(){
jQuery("#art-nav").addClass("clicked");
jQuery("#shop-nav").removeClass("clicked");
jQuery("#obj-nav").removeClass("clicked");
jQuery("#acc-nav").removeClass("clicked");
});
etc .. etc!
HTML
<div id="nav-cell-1" class="grid f-cell nav-cell">
<ul id="main-nav" class="nav clearfix">
<li><a href="#" id="shop-nav">Shop</a>
<ul id="shop-cats">
<li><a href="#" id="art-nav">Art</a></li>
<li>•</li>
<li><a href="#" id="obj-nav">Objects</a></li>
<li>•</li>
<li><a href="#" id="acc-nav">Accessories</a></li>
</ul>
</li>
</ul>
</div>
CSS
a:link, a:visited {color:#cfb199;text-decoration:none}
a:active, a:hover {color:#9d9fa1;text-decoration:none}
a:link.clicked, a:visited.clicked {color:green;text-decoration:underline}
demo site is here:
http://www.tomcarden.net/birdycitynav/partial-nav-demo.html
I solved part of the problem using the link this, but this does not include the part .removeClass.
jQuery('#shop-cats>li>a').click(function(){
jQuery(this).addClass("clicked");
});
source
share