views:

29

answers:

2

Given this website: link text

How would one find an element and remove any class=selected and add it to the correct link? That way when history, models, and the like links will look become selected when clicked upon.

A: 
$("a").click(function(e) {
$("a.selected").removeClass("selected");
$(this).addClass("selected");
});
Liam Bailey
+1  A: 

The following code should do the trick in your case..

// when clicking a link inside the sub-navigation list
$('#sub-navigation a').click(function(){ 
    // first remove the selected class from the active one
    $('#sub-navigation li.selected').removeClass('selected');
    // then find the parent li of the clicked element and add the selected class
    $(this).parents('li').addClass('selected');
});

(it is tested on your example page and it works as expected..)

Gaby
This works perfectly on the above site. Pretty slick stuff man.
A_Var