...

View Full Version : jQuery using show/hide and need toggle clicked link class



stylepile
11-20-2009, 03:37 PM
I am using the script below to show and hide three divs using three links. What I would like to know is how to add a class to the currently clicked link and then remove it when one of the other links is clicked.


$(document).ready(function(){
var divs = $('div.recommended_block, div.buying_advice_block, div.brand_reviews_block').hide();
$('div.recommended_block').show();
$('a#recommended_block, a#buying_advice_block, a#brand_reviews_block').click(function() {
divs.filter(':visible').hide();
$('.' + this.id).show();
return false;
});
});

Fumigator
11-20-2009, 05:21 PM
One way would be whenever a link is clicked, apply the "off" class to all the links, then apply the "on" class to just the clicked link (accessable via $(this)).

tomws
11-20-2009, 05:54 PM
Using Fumigator's suggested method with toggleClass() (http://docs.jquery.com/Attributes/toggleClass) makes this super easy.

Fumigator
11-20-2009, 08:19 PM
Using Fumigator's suggested method with toggleClass() (http://docs.jquery.com/Attributes/toggleClass) makes this super easy.

Oh, right on, good idea. I wouldn't have thought to use that one.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum