Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

Javascript Variable Names

Choosing variables dynamically?

5:42 pm on Jun 17, 2003 (gmt 0)

Preferred Member

10+ Year Member

joined:May 24, 2003
votes: 0

Okay, the Javascript parsing worked perfectly, thanks Tedster and ShawnR.

Now, I have a page full of textboxes (var1, var2, var3,...) I want to use a loop to collect the values of these forms and store them in a single string. There are quite a few, so I didn't want to have a long list of individually referencing each one.

So, is there some way to dynamically choose which variable to access?

7:53 pm on June 17, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 18, 2003
votes: 0

eval("varname = " + value)

Will do it.

To make a (possibly inapplicable) suggestion, it would be better to store all these values in an array instead:

var variables = []

variables["var1"] = 1;
variables["var2"] = 2;


8:43 pm on June 17, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Dec 27, 2002
votes: 0

to follow on from drbrain, another solution could be

var d=document;
var f=your_forms_name;
var concatString="";
var separator="¦";

elevalue= d.f.elements[i].value+"";