ParoX
ParoX

Reputation: 5941

JQuery: Making a tab active

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.

Upvotes: 0

Views: 139

Answers (2)

Gabriele Petrioli
Gabriele Petrioli

Reputation: 196236

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..)

Upvotes: 1

Liam Bailey
Liam Bailey

Reputation: 5905

$("a").click(function(e) {
$("a.selected").removeClass("selected");
$(this).addClass("selected");
});

Upvotes: 0

Related Questions