looking up variables within a loop

windows

Guest
Let's say I have some variables called myvar_1, myvar_2, myvar_3 and so on...:rocker: <br />
<br />
Then on my page I have a for loop something like:<br />
<br />
for (count=1; count<=5; count++)<br />
{<br />
document.write(myvar_1)<br />
}<br />
<br />
How can I get the loop to show myvar_1 on the first loop, then myvar_2 on the second loop then myvar_3 on the third and so on?<br />
<br />
I have tried several methods but I can't seem to get it to work :(<!--content-->for (count=1; count<=5; count++)<br />
{<br />
document.write(myvar_count)<br />
}<!--content-->That's what I thought too but it doesn't work! :( it just looks up a variable called myvar_count and cos it's not there it produces an error.<!--content-->If you want to create the variables during page loading you can simply do as follows:<br />
<br />
for (var count=0;count<=5;count++){<br />
eval("var myVar"+count);<br />
} <br />
<br />
above will declare the variables: myVar1, myVar2, myVar3, etc...<br />
<br />
It would be the same as writing:<br />
var myVar1;<br />
var myVar2;<br />
var myVar3; .. etc<br />
<br />
You could also write: if (eval("myVar"+i+"='five')) to test if any of the variable had a value of 'five' for example.<br />
<br />
If you want to use the document.write method then you have to do so during page loading. if you try to effectuate a document.write on a page which has been fully loaded, the existing page will be cleared from memory. Writing:<br />
<br />
for (var count=0;count<=5;count++){<br />
document.write("var myVar"+count);<br />
} <br />
<br />
is the same as above, but the only difference is that the document.write should only be done during inital load of the page (not after the page has finished loading). It is often safer simply to use "eval" than using document.write to piece together variable references.<br />
<br />
Here is a few tips:<br />
<br />
eval can be used for javascript to evaluate a statement, e.g. to have javascript consider the end product of the enclosed statement as if it was written as such.<br />
<br />
example:<br />
<br />
var myVar0 = "zero";<br />
var myVar1 = "one";<br />
var myVar2 = "two";<br />
var myVar3 = "three";<br />
var myVar4 = "four";<br />
var myVar5 = "five";<br />
<br />
for (var i=0;i<=5;i++){<br />
var tempVar = eval("myVar"+i);<br />
alert(tempVar);<br />
}<br />
<br />
<br />
If you are unsure if a variable exsits in a page you need to specifictly use the windows object to test for it. E.g. to test if the variable 'myVar2' exists you have to ask:<br />
if (window.myVar2){ <br />
.. things to do if it exsits<br />
} else {<br />
.. things to do if it does not exists<br />
}<br />
<br />
If you only wrote: if (myVar2){ ... you would get an error.<br />
Example:<br />
<br />
<br />
var myVar0 = "zero";<br />
var myVar2 = "two";<br />
<br />
for (var i=0;i<=2;i++){<br />
if ( eval("window.myVar"+i){<br />
var tempVar = eval("myVar"+i);<br />
} else {<br />
alert("myVar"+i+" has not been defined");<br />
}<br />
}<!--content-->Thank you!<br />
<br />
That eval method was exactly what I needed! Now everything works!<br />
<br />
Thanks again,<br />
<br />
Seany.<!--content-->
 
Back
Top