Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Nov 2009
    Location
    Milwaukee
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    using show/hide and need toggle clicked link class

    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.

    Code:
    $(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;
    	});
    }); 

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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)).

  • #3
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Using Fumigator's suggested method with toggleClass() makes this super easy.
    Are you a Help Vampire?

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Quote Originally Posted by tomws View Post
    Using Fumigator's suggested method with toggleClass() makes this super easy.
    Oh, right on, good idea. I wouldn't have thought to use that one.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •