...

View Full Version : String as a variable



codefox
01-21-2003, 06:10 AM
Please take a look at this function:

function test(p1, p2)
{
"a" + p1 + "b" + p2 = 10; // If p1 = 1, p2 = 2, variable a1b2 = 10
}

I want "a" + p1 + "b" + p2 to be considered as a variable. I tried eval("a" + p1 + "b" + p2) = 10, but that didn't work. How can I make a string to represent a variable name?

Thanks.

joh6nn
01-21-2003, 06:37 AM
if it's a global variable, then you want

function test(p1, p2) {
window["a" + p1 + "b" + p2] = 10; // If p1 = 1, p2 = 2, variable a1b2 = 10
}

hope that helps

codefox
01-21-2003, 07:04 AM
Thanks, that helped.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum