Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jun 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help with syntax

    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++) {
    menu**i**
    }

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

  • #2
    Regular Coder
    Join Date
    Dec 2003
    Posts
    132
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

    Code:
    for(var i=0;i<3;i++){
    eval("menu"+i+"=1");
    }
    That is, if menu1 2 and 3 are variables...

    hope that helps
    UNDER CONSTRUCTION

  • #3
    New to the CF scene
    Join Date
    Jun 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks!

  • #4
    New to the CF scene
    Join Date
    Jun 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #5
    New Coder
    Join Date
    Jun 2005
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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...

  • #6
    New to the CF scene
    Join Date
    Jun 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •