Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help-a-newbie with variables ...

Status
Not open for further replies.

contiw

Programmer
Jun 28, 2002
25
US
Greetings Javascripters,

Need to build a series of variables on the fly:

for(var i=1;i<=24;i++){
'xxx'+i = ....
}

This doesn't work.
Please advise

 
try using the eval function _________________________________________________________
for the best results to your questions: FAQ333-2924
01001111 01101110 01110000 01101110 01110100
onpnt2.gif
[/sub]
 
Thank You opnt.

Tried:
eval('xxx'+i = '...');
and
eval('xxx'+i) = '...';

with no success.
How is it?

 
<html>
<head>
<script>
var heading = &quot;Option one arrays&quot;;
var myArray = new Array();
for(var i=0;i<=24;i++){ //loop through to 24
myArray = &quot;value &quot; + i; //give avlue to array
eval('var var'+i+'=myArray'); //create variable with array value
alert(eval('var'+i)); //test
document.write(&quot;value &quot;+myArray+&quot; created<br>&quot;);
}
</script>
</html>

two things here. first I would use arrays but if that is something you do not want to do with in the loop I show the variable creation. _________________________________________________________
for the best results to your questions: FAQ333-2924
01001111 01101110 01110000 01101110 01110100
onpnt2.gif
[/sub]
 
a bit more explanation while running through the code
var mag = &quot;&quot;;
var myArray = new Array();
for(var i=0;i<=24;i++){
myArray = &quot;value &quot; + i;
eval('var var'+i+'=myArray');
msg = &quot;var&quot;+i+&quot; variable create with value &quot;+ eval('var'+i);
alert(msg);
document.write(&quot;value &quot;+myArray+&quot; created<br>&quot;);
} _________________________________________________________
for the best results to your questions: FAQ333-2924
01001111 01101110 01110000 01101110 01110100
onpnt2.gif
[/sub]
 
sorry,
var mag = &quot;&quot;;
var myArray = new Array();
for(var i=0;i<=24;i++){
myArray = &quot;value &quot; + i;
eval('var var'+i+'=myArray');
msg = &quot;var&quot;+i+&quot; variable create with value &quot;+ eval('var'+i);
alert(msg);
document.write(&quot;value &quot;+myArray+&quot; created<br>&quot;);
} _________________________________________________________
for the best results to your questions: FAQ333-2924
01001111 01101110 01110000 01101110 01110100
onpnt2.gif
[/sub]
 
alright I quite for the day. this cold is clouding my brain

change that mag declaration to msg. even knowing that doesn't really matter. _________________________________________________________
for the best results to your questions: FAQ333-2924
01001111 01101110 01110000 01101110 01110100
onpnt2.gif
[/sub]
 
if you are just wanting to make the variables and assign them a value- wouldn't this work? maybe I am missing something

var mag = &quot;&quot;;
var myArray = new Array();
for(var i=0;i<=24;i++){
myArray = &quot;value &quot; + i;
document.write(&quot;value &quot;+myArray+&quot; created<br>&quot;);
}
 
that should be, added spaces to [ i ]:

var mag = &quot;&quot;;
var myArray = new Array();
for(var i=0;i<=24;i++){
myArray [ i ] = &quot;value &quot; + i;
document.write(&quot;value &quot;+myArray+&quot; created<br>&quot;);
}
 
this line
eval('var var'+i+'=myArray');
dynamically creates a variable with the value of the array element currently selected

I think that is what they wanted. although seemingly long winded when you can use arrays _________________________________________________________
for the best results to your questions: FAQ333-2924
01001111 01101110 01110000 01101110 01110100
onpnt2.gif
[/sub]
 
[lol]
ok forget the i's and try it again with x's.
this line
eval('var var'+x+'=myArray[x]');
dynamically creates a variable with the value of the array element currently selected
_________________________________________________________
for the best results to your questions: FAQ333-2924
01001111 01101110 01110000 01101110 01110100
onpnt2.gif
[/sub]
 
I do not think you need to eval to create the variable- this will actually create the variable... test by:

var myArray = new Array();
for(var i=0;i<=24;i++){
myArray [ i ] = &quot;value &quot; + i;
document.write(&quot;value &quot;+myArray+&quot; created<br>&quot;);
}
document.write(myArray[7]);



myArray[7] produces &quot;value7&quot;

 
that creates a value of the array element not a variable _________________________________________________________
for the best results to your questions: FAQ333-2924
01001111 01101110 01110000 01101110 01110100
onpnt2.gif
[/sub]
 
help me learn:

if document.write(myArray[7]); outputs &quot;value7&quot; how is that not creating a variable? myArray[7] is a variable (of an array) holding the value of &quot;value7&quot; ???

thanks for the education....

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top