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 3 of 3
  1. #1
    Regular Coder
    Join Date
    Nov 2007
    Posts
    682
    Thanks
    319
    Thanked 1 Time in 1 Post

    Select Variable with String

    How can I use a string to select a variable?

    E.g.
    Code:
    function load_menu(menu) {
    	//Availible menus
    	var login = Array('Login','Register','Recovery');
    	var settings = Array('Profile','Account','something');
    
    	//Menu container ID's
    	var cons = Array('menuitem1','menuitem2','menuitem3');
    	
    	for ( var i = 0; i < cons.length; i++ ) {
    		document.getElementById(cons[i]).innerHTML = menu[i];
    		alert(menu[i]);
    	}
    }
    "menu" is "login".
    menu[i] should reference the "login" variable and then put one of the values for the login variable array in its place.

    If you understand what I mean?
    So if menu is settings then the settings array would be used.

  • #2
    New Coder
    Join Date
    Jul 2010
    Posts
    61
    Thanks
    0
    Thanked 21 Times in 21 Posts
    I think this fits what you're attempting to do:
    Code:
    function load_menu( menu ) 
    {
      var data = [];
    	
      data[ 'login' ] = [ 'Login', 'Register', 'Recovery' ];
      data[ 'settings' ] = [ 'Profile', 'Account', 'something' ];
    
    	//Menu container ID's
      var cons = Array('menuitem1','menuitem2','menuitem3');
    
      for ( var i = 0; i < cons.length; i++ ) 
       document.getElementById(cons[i]).innerHTML = data[ menu ][ i ];
    }

  • Users who have thanked RandomUser531 for this post:

    martynball (07-25-2010)

  • #3
    Regular Coder
    Join Date
    Nov 2007
    Posts
    682
    Thanks
    319
    Thanked 1 Time in 1 Post
    That's perfect, I never thought of doing that Lol. Thanks


  •  

    Posting Permissions

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