homepage Welcome to WebmasterWorld Guest from 54.211.95.201
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Javascript Variable Names
Choosing variables dynamically?
TGecho




msg:1479478
 5:42 pm on Jun 17, 2003 (gmt 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?

 

drbrain




msg:1479479
 7:53 pm on Jun 17, 2003 (gmt 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;

etc.

HocusPocus




msg:1479480
 8:43 pm on Jun 17, 2003 (gmt 0)

to follow on from drbrain, another solution could be

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

for(i=0;i<d.f.elements.length;i++){
if(d.f.elements[i].name.substring(0,2)=="var"){
elevalue= d.f.elements[i].value+"";
concatString=concatString+elevalue+separator;
}}

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved