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
    New Coder
    Join Date
    Aug 2011
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Any idea why this isn't working?

    Code:
    <ul>
        <li><a class="changing" href="#magic">click!</a></li>
        <li><a class="changing" href="#otherMagic">click!</a></li>
    </ul>
    
    <div id="magic" class="visible">Now you see me</div>
    <div id="otherMagic" class="invisible">Now you see something else</div>
    JS
    Code:
    $(document).ready(function() {
    	$('.changing').click(function(){
    		$(this.href).removeClass("invisible").addClass("visible");
    	});
    });
    I'm not sure what's wrong here, but for some reason when the links with "changing" class are clicked, they dont add the visible class. visible and invisible are just classes with "visibility:visible;" and "visibility:hidden;" respectibly. Any ideas? =\

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,084
    Thanks
    23
    Thanked 593 Times in 592 Posts
    Not sure what you want to do exactly but I do know you want
    Code:
    $(this).attr('href')
    You really don't need to set a class to visible and a toggle works fine.

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title>Title of the document</title>
    <style type="text/css">
    .invisible{
    	visibility:hidden;
    }
    </style>
    </head>
    
    <body>
    <ul>
        <li><a class="changing" href="#magic">click!</a></li>
        <li><a class="changing" href="#otherMagic">click!</a></li>
    </ul>
    
    <div id="magic">Now you see me</div>
    <div id="otherMagic" class="invisible">Now you see something else</div>
    
    <script type='text/javascript' src='javascript/jquery.js'></script>
    <script type="text/javascript">
    $(document).ready(function() {
    	$('.changing').click(function(){
    		temp = $(this).attr('href');
    		$(temp).toggleClass("invisible");
    
    	});
    });
    </script>
    </body>
    </html>
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #3
    New Coder
    Join Date
    Aug 2011
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Indeed
    Code:
    $(this).attr('href')
    and toggle did the trick! thanks sunfighter!


  •  

    Posting Permissions

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