...

View Full Version : Get Array element through New Variable



sybaritic_life
08-13-2004, 10:11 PM
I keep getting the error "undefinedArray is undefined" when I run this this block of code even though I am able to access the array element. I think it has to do with the way I'm accessing the array. Can you help?


function ClearMenu(RO,position)
{
var string = '<img src="./images/nav/WhiteTile.gif" width="1" height="1" border="0"></div>';
var output = '<h3>' + eval(RO+'Array['+position+']') + '</h3>';

Willy Duitt
08-13-2004, 10:52 PM
Obviously your problem lies with your defining of the array here: eval(RO+'Array['+position+']')

But considering that you only posted a small snippet of the code and I do not know what the Array name is suppose to look like, all I can do is quess...

Try this: eval(RO+Array[position])

But then again, I would nver use eval....

.....Willy

sybaritic_life
08-13-2004, 11:25 PM
The array is:

RO1Array = new Array("Tutoring: Grades 1-6", "Tutoring: Grades 7-12", "Drop-In Classes", "Books, Supplies Materials");

I actually figured out the reason for the undefinedArray error. I was calling a function somewhere else in the script without passing the right arguments.
Thank you for your help--can you suggest another function to use other than eval()? Why is it so evil?

JohnKrutsch
08-14-2004, 05:42 AM
Try this:

var output = '<h3>' + window[RO+"Array"][position] + '</h3>';

Matt RW
08-14-2004, 10:51 AM
Try this:

var output = '<h3>' + window[RO+"Array"][position] + '</h3>';
looks to me like it should be...

var output = '<h3>' + window["RO"+RO+"Array"][position] + '</h3>';

JohnKrutsch
08-16-2004, 12:28 AM
That all depends on what the value of RO is. Based on previous posts it is suggested that the eval method worked but sybaritic_life was just looking for a way to do it without eval. If that was the case then the way I suggested will work just fine.

glenngv
08-16-2004, 05:24 AM
Why is it so evil?
eval()...pros and cons (http://www.codingforums.com/showthread.php?t=20143&highlight=eval+evil)

sybaritic_life
08-16-2004, 03:34 PM
Thanks all for very useful comments & shedding some light. window["RO"+RO+"Array"][position]works great.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum