Results 1 to 7 of 7

Thread: Simplifying some simple code

  1. #1
    Join Date
    Apr 2006
    Posts
    205
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Smile Simplifying some simple code

    Hello!

    Here's a simple bit of code.

    Code:
    function selectLink(elementName) {
    	
    	obj = document.getElementById(elementName);
    	obj.className = "selected"; 
    	
    }
    It adds the class 'selected' to a link in my menu.

    I call it when the pages loads, like this:
    HTML Code:
    <body onload="selectLink('aboutLink1')">
    It works because I've given the links IDs, like this:

    HTML Code:
        <ul class="greyMenu1">
         <li><a href="../about" id="aboutLink1">About</a></li>
         <li><a href="../contact" id="contactLink1">Contact</a></li>
         <li><a href="../purchase" id="purchaseLink1">Purchase</a></li>
        </ul>
    The thing is, I've got another menu that I also want to use the javascript on, it looks like this:

    HTML Code:
       <ul class="greyMenu1">
         <li><a href="../about" id="aboutLink2">About</a></li>
         <li><a href="../contact" id="contactLink2">Contact</a></li>
         <li><a href="../purchase" id="purchaseLink2">Purchase</a></li>
       </ul>
    So, in the onload command I'm calling the javascript once for the first menu and once for the second, like this:
    HTML Code:
    <body onload="selectLink('aboutLink1'),selectLink('aboutLink2')">
    What I'd like to do is change the javascript so that it can deal with both the elements at once. Effectively I'd like to reduce my HTML to this:
    HTML Code:
    <body onload="selectLink('aboutLink1','aboutLink2')">
    However, I don't know how to change the javascript to have it deal with more than one element in one go. If anyone knows how, I would really appriciate the lesson.

    Thanks,

    Dog

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Code:
    function selectLink(Eone, Etwo) {
    	
    	document.getElementById(Eone).className = 'selected';
    	document.getElementById(Etwo).className = 'selected';	
    }
    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. The Following User Says Thank You to thetestingsite For This Useful Post:

    dog (03-29-2008)

  4. #3
    Join Date
    Apr 2006
    Posts
    205
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Thumbs up

    Wow that was amazingly fast and it works too!

    It even works if I only define one element, like this:
    HTML Code:
    <body onload="selectLink('aboutLink2')">
    ...does doing this cause any errors? Doesn't the javascript expect to recieve two elements?

    Another question, just for the sake of learning a bit more about javascript:

    Assuming I had a massive (and variable) number of elements to deal with, as opposed to always having just two, what alternative code could be used?

    Thanks again.

  5. #4
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    Code:
    function selectLink(/*ids*/) {
      for(var i = arguments.length - 1; i >= 0; --i)
        document.getElementById(arguments[i]).className = "selected";
    }
    Code:
    // examples
    
    selectLink(); // nothing
    selectLink("id1");
    selectLink("id2", "id3");
    selectLink("id4", "id5", "id6");
    
    var idArray = [/*bunch of ids*/];
    selectLink.apply(null, idArray);
    Trinithis

  6. The Following User Says Thank You to Trinithis For This Useful Post:

    dog (03-29-2008)

  7. #5
    Join Date
    Apr 2006
    Posts
    205
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Thumbs up

    Your code is very good. Thank you!

    I put just the first bit in place:

    Code:
    function selectLink(/*ids*/) {
      for(var i = arguments.length - 1; i >= 0; --i)
        document.getElementById(arguments[i]).className = "selected";
    }
    To be quite honest I didn't understand the second section of code and I'm not sure what I should do with it.

    Please, if anyone is of a mind to, feel free to explain any of this code to me.

    Eitherway, thanks a lot for all the help. Aside from a bit of increased understanding I've got just what I was looking for.

    Peace out

  8. #6
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    You can pass any number of arguments to a javascript function.

    arguments is an array (technically, just array-like) structure that stores each argument passed to the function.

    Code:
    arguments.length == the number of arguments passed to the function.
    
    arguments[0] == the first argument (if any)
    arguments[1] == the second argument (if any)
     . . .
    arguments[argument.length-1] == the last argument (if any)
    Trinithis

  9. #7
    Join Date
    Apr 2006
    Posts
    205
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Smile

    Cool! Thanks man.

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
  •