View Full Version : Help with syntax

06-03-2005, 08:46 PM
I haven't used javascript very much and I've run into an issue.

I am trying to access variables from a menu javascript (HV Menu) in a different script. Menus in the HV Menu script are written out "Menu1", "Menu2", etc. I would like to use a for loop to access each menu, sort of like this:

for (i=1; i<=5; i++) {

Could I somehow use a loop to access Menu1, Menu2, Menu3 etc. instead of writing each name out?

06-03-2005, 09:28 PM
well, I don't know what an hv menu is but i would say use the handy
eval() method. It takes a string and executes it as javascript. like so:

for(var i=0;i<3;i++){

That is, if menu1 2 and 3 are variables...

hope that helps

06-03-2005, 10:36 PM

06-04-2005, 06:42 AM
Now I am having a new problem. I am trying to use a for loop inside the eval function, but there seems to be a problem when I use i++. When I test it out, i never increases like it should--it just stays at 1. How can I get i++ to work in the eval function?

06-04-2005, 07:23 AM
I think it may help if we can see some code... i.e. the for loop your using, and how / what your trying to do with the information... i.e. if your building an array list of say array('menu1','menu2','menu3') then this would be easier to comment on, also how is this array being used afterwards..

per above...

array = array();

for i ....
if (whatever)
array[i] = 'menu'+i

now you know which menus to act on so

for j ...
print array[j] etc...

06-04-2005, 08:30 AM
Well, it seems we are not understanding the same thing here, and that's my fault for not putting any code. Anyhow, I figured out that, at least the way I was doing it, i++ will not work in a eval() function. I just ended up storing the items I needed to eval in variables and using them in a normal for loop, instead of writing the loop inside the eval() parameters. Thanks, both of you, for helping out.