Not signed in (Sign In)

SkillShare - A place to discuss Web Standards and Web Design topics

Categories

Vanilla 1.1.5a is a product of Lussumo. More Information: Documentation, Community Support.

  1.  permalink
    Hi,

    I am just scratching the surface of JavaScript and want to make sure I'm doing it right, rather than learning it wrong.

    Here is what I need to do:

    I am looking to have JavaScript check the browser location "http://www.re-volvemedia.com/link_name.php" if that matches any of the links in the <ul id="nav"> links then it will set the class of that matching link to <a href="matching_link.php" class="class_name_current">.

    Here is what I've done, but I don't know where to go from here.

    var w = window.location.href;
    var n = document.getElementById("nav");
    var l = n.getElementByTag("a");

    function setCurrent()
    {
    if(w == l)
    {
    Code to set the class name to "_current";
    }
    }

    What I have is the variable "w" is checking the window location, the variable "n" is getting the "nav" ID, and the variable "l" is taking the id "nav" and finding all tags "a"...maybe. I'm trying to think this out logially rather than finding a quick solution. If anyone can offer suggestions that would rock!

    Thanks,
    Seth
    •  
      CommentAuthorJohnRiv
    • CommentTimeSep 28th 2006 edited
     permalink
    This code should do the trick:

    var w = window.location.href;
    var n = document.getElementById("nav");
    var l = n.getElementsByTagName("a");
    for(i=0;i<l.length;i++) {
    if(l[i].href == w) {
    l[i].className = "class_name_current";
    }
    }


    Note that there is no "getElementByTag" in JavaScript. It's getElementsByTagName
  2.  permalink
    Just to make sure what you did so I can know how in the future.

    You fixed the TagName and ran a loop to see all the links. Then you made an if statement that says if link.href is equil to the location take that link.className to equil "current"

    Would it be possible to add to the class with this line?

    [CODE]
    l.className = className + "_current";
    [/CODE]

    How do you use the BBCode tags to show code?
    •  
      CommentAuthorJohnRiv
    • CommentTimeSep 28th 2006
     permalink
    yes, if the class of the links is "class_name", then you could make the link class become "class_name_current" by simply doing:

    l[i].className = l[i].className + "_current";

    or to shorten that line of code slightly, this will do the same thing:

    l[i].className += "_current";
Add your comments
    Username Password
  • Format comments as (Help)