Results 1 to 6 of 6

Thread: change then remove class with javascript

  1. #1
    Join Date
    Apr 2007
    Posts
    149
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default change then remove class with javascript

    Hello guys, i have a small question:

    i have the following code which i use to change the class for selected state on a navigation bar:

    Code:
    <style type="text/css">
    
    #navigationDiv a {
       text-decoration:none;
       display:block;
       color:black;
       margin-bottom:5px;
    }
    
    
    
    #navigationDiv a.selected {
       text-decoration:underline;
    }
    
    </style>
    
    <script type="text/javascript">
    
    function highlightLinks(obj) {
       var linkList = document.getElementById("navigationDiv").getElementsByTagName("a");
       for (i = 0; i < linkList.length; i++) {
          linkList[i].className = "";
       }
       obj.className = "selected";
    }
    
    </script>
    
    <div id="navigationDiv">
    <a href="#" onclick="highlightLinks(this)" class="">anchor1</a>
    <a href="#" onclick="highlightLinks(this)" class="">anchor2</a>
    <a href="#" onclick="highlightLinks(this)" class="">anchor3</a>
    <a href="#" onclick="highlightLinks(this)" class="">anchor4</a>
    <a href="#" onclick="highlightLinks(this)" class="">anchor5</a>
    <a href="#" onclick="highlightLinks(this)" class="">anchor6</a>
    <a href="#" onclick="highlightLinks(this)" class="">anchor7</a>
    <a href="#" onclick="highlightLinks(this)" class="">anchor8</a>
    </div>

    Now, my problem is that when i click another link, the previous on stays with the "selected" class, how can i modify the javascript to remove the class after i click on another link?

    Thank you in advance for your response.
    Last edited by remp; 11-05-2010 at 03:20 PM.

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    For me the link loses the class when another link is clicked. What browser are you viewing it in?
    Jeremy | jfein.net

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Yeah, right. Unless you mean another link somewhere else on the page that isn't there, do you?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Apr 2007
    Posts
    149
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Thanks for your reply guys. I have posted a link so you can see what im talking about:

    http://72.167.86.85/site11/site2/test.html

    When i click on the navigation on the right, i want the selected one to stay orange and the other ones to stay black. but they do not change back to black when clicking on another link.

    Take a look a the page and give me a hand please. Thank you very much for your assistance.

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    When I saw the source code of that page I almost had to laugh. Three external script tags to jQuery (fortunately all the same version), and cufon! Cufon has been known to mess things up and cannot be tested in a local mock up without downloading all of the fonts. There are also some script errors.

    But it looks like you're not really using cufon, and that even if you are, it might not be a factor (Note: further testing shows cufon is being used and that it does cause problems). The other script errors appear not to affect this one thing.

    To get rid of cufon, remove:

    Code:
    	<script type='text/javascript' src='javascript/cufon-yui.js'></script>
    	<script type='text/javascript' src='javascript/nevis_700.font.js'></script>
    and:

    Code:
    	<script type="text/javascript">
                Cufon.replace('h2, h3, h5, .footer, #nav', { hover: 'true' });
        
        </script>
    To make a long story short, the main problem is that you have two elements with the id 'rotator'. In javascript, the parser will at best find the first and use it, at worst throw a fatal script error.

    The 'rotator' element without your links in it is first, so the one you want to target never gets processed.

    Since you use jQuery, you can get around that by changing:

    Code:
            <script type="text/javascript">
    function highlightLinks(obj) {
       var linkList = document.getElementById("rotator").getElementsByTagName("a");
       for (i = 0; i < linkList.length; i++) {
          linkList[i].className = "";
       }
       obj.className = "selected";
    }
    </script>
    to:

    Code:
            <script type="text/javascript">
    function highlightLinks(obj) {
       var linkList = $('#rotator a');
       for (i = 0; i < linkList.length; i++) {
          linkList[i].className = "";
       }
       obj.className = "selected";
    }
    </script>
    But this is a pretty dirty solution. Considering the rest of the problems with the page, it's perhaps the best that can be hoped for. These other problems should be addressed though.
    Last edited by jscheuer1; 11-04-2010 at 06:08 AM. Reason: Looks like cufon is a problem
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Apr 2007
    Posts
    149
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Thanks for you help jscheuer1 (and criticism... hahaha) i don't know that much about jquery and i have to do it this way, but thanks.

    I now realize that Cufon does give problems, and it seem by removing it, the links work as intended but I really like what cufon does and i want to keep it, so i may try another way of accomplishing what it does... Thanks
    Last edited by remp; 11-05-2010 at 03:20 PM.

Bookmarks

Posting Permissions

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