Connecting Tech Pros Worldwide Forums | Help | Site Map

Using dynamic names for objects in JS

Newbie
 
Join Date: Jun 2007
Posts: 3
#1: Jun 8 '07
Hi everyone!
I am an amateur JS programmer and I am stuck at a point. I would be glad if someone suggests me a solution. The problem goes like this...

I have a set of textboxes, who's names I am assigning as "Text1", "Text2" and so on with the help of a loop (using the setAttribute feature).

Now, at the time of submitting the information to the server, I need to validate the fields. If I'd kept the name say "Text", then I can access it as document.form_name.Text.value. In my case, how can i access the textbox object's value, whose name is dynamically changing ?

I even tried out this...
temp = Text + (looping variable);
document.form_name.temp.value....But it doesn't work..!! I am almost on the verge of completion of my project...I am stuck here...Could anyone please suggest me a solution..?

Thanks in advance.

gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,134
#2: Jun 8 '07

re: Using dynamic names for objects in JS


Quote:

Originally Posted by gobi517

...
I even tried out this...
temp = Text + (looping variable);
document.form_name.temp.value
...

hi ... you've nearly got it ;) use the following:

Expand|Select|Wrap|Line Numbers
  1. document.form_name[temp].value
  2.  
kind regards ...

ps: explaination is, that with the . you refer a property but you would refer a property named 'temp' ... so you have to use the array-like reference when refering a property that is variable
Reply