SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    He's No Good To Me Dead silver trophybronze trophy stymiee's Avatar
    Join Date
    Feb 2003
    Location
    Slave I
    Posts
    23,424
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)

    Array not populating

    Code:
    var labels;
    var labelsArray = new Array();
    labels = document.getElementsByTagName("label");
    for (var i=0; i < labels.length; i++) {
        labelsArray[labels[i].getAttribute("id")] = labels[i];
    }
    For some reason the labelsArray array is not being assigned the value of labels[i] (which should be an properties of a label object). i is incrementing but the size of the labelsArray does not grow. Can anyone see something I may have overlooked?

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Portsmouth UK
    Posts
    1,499
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    not sure what you are trying to do but have alook at

    PHP Code:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
        "http://www.w3.org/TR/html4/strict.dtd"
    >

    <
    html>

    <
    head>
      <
    title></title>
    <
    script language="JavaScript" type="text/javascript">
    <!--
    function 
    getLables(){
    var 
    labels;
     
    labelsArray = new Array();
     
    labels document.getElementsByTagName("label");
    for (var 
    i=0labels.lengthi++) {
     
    labelsArray[i]=labels[i].id;
    }
    document.Show.Show2.value=labelsArray;
    }
    //-->
    </script></head>

    <body onload="getLables();" >
    <script language="JavaScript" type="text/javascript">
    <!--
    vic=0;
    document.write('<style> .debug {VISIBILITY: visible; POSITION: absolute; TOP: 500px; z-Index:100; }</style>')
    <!---->
    </script>
    <div class=debug>
     <form name="Show">
     <input type="text" size="10" name="Show1" value="Show1">
     <input type="text" size="10" name="Show2" value="Show2">
     <input type="text" size="10" name="Show3" value="Show3">
     <input type="text" size="10" name="Show4" value="Show4">
     <input type="text" size="10" name="Show5" value="Show5">
     <input type="text" size="10" name="Show6" value="Show6">
     </form>
    </div>


    <label id=tom>
    <label id=fred>

    </body>

    </html> 
    just noticed your posts & mentor bit so I must be missing the point

  3. #3
    He's No Good To Me Dead silver trophybronze trophy stymiee's Avatar
    Join Date
    Feb 2003
    Location
    Slave I
    Posts
    23,424
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by vwphillips
    just noticed your posts & mentor bit so I must be missing the point
    The Mentor part just means I know a couple of things the staff thinks might be helpful to others. Unfortunately JavaScript isn't one of them. The post count just means I need to step away from my computer a little bit more.

    I took a peek at your code and wasn't exactly what I was working at but darn close. I basically was trying to simulate a hash table so I wouldn't have to keep looping through an array later on in my script (this will save me about 50+ loops at 50 iterations per loop. Should boost performance of the script). The problem is for some reason the assignment doesn't seem to be working. This line:

    Code:
    labelsArray[labels[i].getAttribute("id")] = labels[i];
    doesn't seem to do anything.

    Eventually I wll be accessing this array in this manner:

    Code:
    function changeLabel(ele, color) {
        labelsArray[ele.name].style.color = color;
    }
    
    var ele = document.forms[0].elements;
    for (i=0; i < ele.length; i++) {
        changeLabel(ele[i], "black");
    }
    I could post the whole code if it would be helpful. I was trying to avoid it because it's quite a bit to chew on and I think all of the code above is all that is relevant.

  4. #4
    SitePoint Addict
    Join Date
    May 2004
    Location
    Europe
    Posts
    216
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The length of an JS Array only increases if you set an integer property X and X > length, e.g.

    arr = new Array(); // arr.length is 0
    arr[5] = true; // arr.length is 6
    arr["8"] = false; // arr.length is 9

    but
    arr = new Array(); // arr.length is 0
    arr["someid"] = true; // arr.length is still 0

    Just modify your code a bit and the array's length will be the number of labels:
    Code:
    var labels = document.getElementsByTagName("label");
    var labelsArray = new Array(labels.length);
    You don't even need to use an Array because you are not using any of its special features, an ordinary object would do. But quite frankly, I don't see what a hash table with the element id's as the keys is supposed to be good for, that's what document.getElementById does. Also, label elements are not included the form.elements collection. If all you want to do is change the color of all labels in a form, then try this:
    Code:
    var labels = document.forms[0].getElementsByTagName("label");
    for (i=0; i < labels.length; i++) {
        labels[i].style.color = "black";
    }

  5. #5
    He's No Good To Me Dead silver trophybronze trophy stymiee's Avatar
    Join Date
    Feb 2003
    Location
    Slave I
    Posts
    23,424
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    Thanks for the explanation. Very informative and helpful. What was great was how you simplified my solution for me. I was doing much more work then I needed to do. Your code to change the color of all label tags was ideal for me.


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
  •