Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Help with variable in a loop

    I have a lot of variables (lab0, lab1, lab2, ...). How can I use these vaiable in a loop. Javascript thinks it is a variable 'labi'. Is there a way to fix that

    for ( var i=0;i<10; i++ )
    {
    f1.path.op[i].value = f1.labi.value;
    }

    // when i = 0, I want to see lab0
    // when i=1, I I want lab1
    // ...

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    I have a lot of variables (lab0, lab1, lab2, ...). How can I use these vaiable in a loop. Javascript thinks it is a variable 'labi'. Is there a way to fix that
    Try to convey your actual requirement, which may help us to provide an elegant solution.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by abduraooft View Post
    Try to convey your actual requirement, which may help us to provide an elegant solution.
    Basically I am trying to make the following statement in a for loop. Can anyone help?
    By doing that how should represent lab0, lab1, lab2,... . ( lab${i} like in perl ? )

    var lab0,lab1,lab2,lab3
    f1.path.options[0].value = f1.lab0.value;
    f1.path.options[1].value = f1.lab1.value;
    f1.path.options[2].value = f1.lab2.value;
    f1.path.options[3].value = f1.lab3.value;
    ....

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,869
    Thanks
    79
    Thanked 4,421 Times in 4,386 Posts
    Ummm...

    Code:
    var lab = [ "once", "upon", "a", "midnight", "dreary" ];
    for ( var i = 0; i < lab.length; ++i )
    {
        f1.path.options[i].value = lab[i];
    }
    But the use of ".value" in your "f1.lab2.value" makes me wonder what these "labN" things really are.

    Are they form fields????

    Is f1 a <form> reference??

    If so:
    Code:
    for ( var i = 0; i < 999999; ++i )
    {
        var from = f1.elements["lab" + i];
        if ( from == null ) break;
        f1.path.options[i].value = from.value;
    }
    And you don't even have to know how many there are.

    Or is there some reason you opted to *NOT* use an array for lab???
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,869
    Thanks
    79
    Thanked 4,421 Times in 4,386 Posts
    Come to think of it, something along those lines should work no matter what f1 is:

    Code:
    for ( var i = 0; i < 999999; ++i )
    {
        var from = f1["lab" + i];
        if ( from == null ) break;
        f1.path.options[i].value = from.value;
    }
    I'm more than a little curious why you are changing the .value of a some (apparent) <option>s without changing what the user sees (that is, without changing the .text as well).
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    me94086 (12-16-2009)

  • #6
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks so much. It works
    Yes f1 is a form name( reference).
    Sorry I've just put a piece of the my javascript but you are right I need to have something like

    for ( var i = 0; i < 999999; ++i )
    {
    var from = f1["lab" + i];
    if ( from == null ) break;
    f1.path.options[i].text = from.value;
    f1.path.options[i].value = from.value;
    }

    my CGI code has this piece of code
    $i =0;
    foreach (@all) ### @all = ("/h/user1","/h/user203","/h/users12",...);
    {
    print "<input type='hidden' name='lab$i' value=$all[$i]>";
    $i++;
    }

    <select name='path' onchange=whichpath()>
    <option></option>
    </select>
    Last edited by me94086; 12-16-2009 at 05:16 AM.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,869
    Thanks
    79
    Thanked 4,421 Times in 4,386 Posts
    If that f1.path does not already have the same number (or more) of <option>s than there are lab## elements, then that code won't work.

    I think you *probably* want this:
    Code:
    for ( var i = 0; i < 999999; ++i ) /* loop goes forever, almost */
    {
        var from = f1["lab" + i]; /* try to get a reference to a "lab##" element
        if ( from == null ) break; /* if we ran out of them, break out of the loop */
        f1.path.options[i] = new Option( from.value, from.value ); /* the way to create new or replacement options */
    }
    And though
    Code:
        var from = f1["lab" + i];
    should work, the more conventional way to get a form element (or at least the old-fashioned way I learned first) is:
    Code:
        var from = f1.elements["lab" + i]; /* forms have elements, other things don't */
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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