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 3 of 3
  1. #1
    Banned
    Join Date
    Mar 2007
    Posts
    1,523
    Thanks
    116
    Thanked 0 Times in 0 Posts

    Question .addClass() adds a class but stays there?!

    hi,

    in my code below shows a class being added from the start on the first page_subtitle and then each time I click another they get a 'selected' style.

    My problem is is that the first one always has the selected class and when i click on others I it stays on them?!

    JQuery
    Code:
    $(function(){
    
    $(".museums.depth1 .pageblocktext").hide();
          $(".museums.depth1 .pageblock").each(function(){
          	$(this).find("pageblocktext").hide();
          	$(this).find(".page_subtitle").click(function(){
          		$(".museums.depth1 .pageblocktext").hide();
          		$(this).next(".pageblocktext").show();
     			$(this).next(".pageblocktext").addClass("selected");     		
          	});
          });
         
     $(".museums.depth1 .page_subtitle:first").click();
     $(".museums.depth1 .page_subtitle:first").addClass("selected");
    
    });
    HTML
    Code:
    <div class="pageblocks"><div class=" editable pageblock count1" style="background-color: transparent;">
    	<h2 class="page_subtitle selected">About</h2>
    
    
    </div>
    	
    <div class=" editable pageblock count2" style="background-color: transparent;">
    	<h2 class="page_subtitle">Opening Times</h2>
    
    </div>
    	
    <div class=" editable pageblock count3">
    	<h2 class="page_subtitle">Facilities</h2>
    	
    </div>
    <div class=" editable pageblock count4">
    <h2 class="page_subtitle">Contact</h2>
    
    </div>
    	
    </div>

  • #2
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,797
    Thanks
    6
    Thanked 1,023 Times in 996 Posts
    Yeah, of course you need to remove the class from the other element(s) upon clicking the element.

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,412
    Thanks
    11
    Thanked 595 Times in 575 Posts
    Code:
     $(".museums.depth1 .page_subtitle:first").addClass("selected");
    can be

    Code:
     $(".museums.depth1 .selected").removeClass("selected");
     $(".museums.depth1 .page_subtitle:first").addClass("selected");
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%


  •  

    Posting Permissions

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