...

View Full Version : Help with variable in a loop



me94086
12-15-2009, 09:25 AM
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
// ...

abduraooft
12-15-2009, 09:30 AM
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.

me94086
12-15-2009, 09:45 AM
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;
....

Old Pedant
12-16-2009, 01:15 AM
Ummm...



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:


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???

Old Pedant
12-16-2009, 01:18 AM
Come to think of it, something along those lines should work no matter what f1 is:



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).

me94086
12-16-2009, 06:04 AM
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>

Old Pedant
12-16-2009, 07:20 AM
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:

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

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:

var from = f1.elements["lab" + i]; /* forms have elements, other things don't */



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum