SitePoint Sponsor

User Tag List

Page 2 of 2 FirstFirst 12
Results 26 to 29 of 29

Thread: number of elements in array

  1. #26
    SitePoint Mentor bronze trophy
    chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,041
    Mentioned
    9 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by tlacaelelrl View Post
    WOW, I canīt beleive this, the spelling was my problem oh well, next time when something does not work as expected I will step back and look closely before I ask any more of this stupid questions.

    Thank you
    No question is ever stupid, some of the SitePoint mentors and staff even ask questions because no matter what you know a challenge always lays waiting in the road ahead.
    Blog/Portfolio | Evolution Xtreme | DFG Design | DFG Hosting | CSS-Tricks | Stack Overflow | Paul Irish
    Having lame problems with your code? Let us help by using a jsFiddle

  2. #27
    SitePoint Enthusiast
    Join Date
    Jun 2011
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    length attribute will give you the length of the array, just type arrayname.length

  3. #28
    SitePoint Addict tlacaelelrl's Avatar
    Join Date
    Apr 2011
    Location
    Mexico city, Mexico
    Posts
    345
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    What would you say should be used?

    this

    Code JavaScript:
    function get_active_link(myul, myclass){
    var menu_unidades_yolo = document.getElementById(myul);
    menu_unidades_yolo = menu_unidades_yolo.getElementsByTagName('a');
    var number=menu_unidades_yolo.length;
    var current_url = document.location;
    while (number >= 0){
    if (menu_unidades_yolo[number].href == current_url){
    menu_unidades_yolo[number].className = myclass;
    }
    number--;
    }
    }

    or this

    Code JavaScript:
    function get_active_link(myul, myclass){
    var menu_unidades_yolo = document.getElementById(myul);
    menu_unidades_yolo = menu_unidades_yolo.getElementsByTagName('a');
    var current_url = document.location;
    for (var i in menu_unidades_yolo){
    if (menu_unidades_yolo[i].href == current_url){
    menu_unidades_yolo[i].className = myclass;
    }
    }
    }

  4. #29
    Unobtrusively zen silver trophybronze trophy
    SitePoint Award Recipient paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,203
    Mentioned
    40 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by tlacaelelrl View Post
    What would you say should be used?
    The while loop is better than the for...in one, but for the easiest to understand code you should use a normal for loop to iterate through the array items.

    Code javascript:
    function get_active_link(myul, myclass){
        var menu_unidades_yolo = document.getElementById(myul).getElementsByTagName('a'),
            linksLen = menu_unidades_yolo.length,
            current_url = document.location,
            i,
            link;
        for (i = 0; i < linksLen; i++) {
            link = menu_unidades_yolo[number];
            if (link.href == current_url) {
                link.className = myclass;
            }
        }
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

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
  •