...

View Full Version : Select Variable with String



martynball
07-24-2010, 10:28 PM
How can I use a string to select a variable?

E.g.


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.

RandomUser531
07-25-2010, 04:00 AM
I think this fits what you're attempting to do:


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 ];
}

martynball
07-25-2010, 12:37 PM
That's perfect, I never thought of doing that Lol. Thanks :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum