UPDATE2: this is not a javascript problem
<li><a href="#"><span <%=ServerSizeLogic.SelectedMenu("Home")%>>Home</span></a></li>
This is a nasty guess on how the server side might to it, depending on your server side technology. ServerSizeLogic.SelectedMenu("Home")
or whatever you use would either return an empty string or "class='test'". Just make sure only one of your li elements gets the style and remove the client side code
// don't do it like this but hopefully you get the gist
ServerSizeLogic::SelectedMenu(string item){
if (item == CurrentPage)
return "class='test'";
return "";
}
UPDATE: obviously once you navigate away from the page the element you want to be selected will have to come from the server (unless you are using ajax), in which cast just get rid of the li:first-child
and set the class directly in your view code on the current li
element
Original:
How about this little fiddle http://jsfiddle.net/LmUrP/
$(function () {
$("li:first-child").addClass("test");
$('li').click(function () {
$('#menu li').removeClass('test');
$(this).addClass('test');
});
});
html
<ul id='menu'>
<li><a href="#"><span>Home</span></a></li>
<li><a href="#"><span>contact us</span></a></li>
<li><a href="#"><span>apples</span></a></li>
<li><a href="#"><span>bananas</span></a></li>
</ul>