Welcome to WebmasterWorld Guest from 3.80.6.254

Forum Moderators: open

Message Too Old, No Replies

Concatenation Issue

     
6:45 am on Oct 31, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Sept 26, 2003
posts:141
votes: 0


I am trying to access elements of a form and I have been unable to do so.

I am using the following variables in a for loop to access variables based on the "i" value which starts at 1 and continues until the for loop meets a specified variable.

The issue below is that it literally produces the string value.

eg. salutation="x.salutation"+i+".value"
after execution salutation will contain x.salutation1.value (assuming i was 1 at this point)

What I would like to be assigned is the actual value retrieved by x.salutation1.value

Can someone please help me find a nice solution here?

7:42 am on Oct 31, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:July 26, 2003
posts:881
votes: 0


One way is (only works in IE I think)...

var salutation = null;
eval("salutation=x.salutation"+i+".value");

Another way is (all browsers I think)...

var salutation = null;
eval("salutation=window.forms['x'].salutation"+i+".value");

Best way (imo) is to set an id="salutation1", &c, on your form entrys, then use...

salutation=document.getElementById("salutation"+i).value;

Jordan

9:38 am on Oct 31, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Sept 26, 2003
posts:141
votes: 0


Thanks Jordan

The first method works fine and it also seems to work just the same in netscape.

However, the second method that sounds better since you think it works for all browsers doesn't work. It actually produced the same results as my initial code for some reason.

var salutation = null;
eval("salutation=window.forms['x'].salutation"+i+".value");

In the above code does the 'x' assume my form is called "x" or is it that it assumes the reference 'x' refers to a live form? I have tried 'x' and also the actual form name 'reg2' just to make sure I wasn't making a mistake.

I haven't tested the third 1 because my radio buttons haven't been coded with ids.

7:18 pm on Oct 31, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:July 26, 2003
posts:881
votes: 0


If the first one works then I'm probably just making things more complicated than needs be with the second. :)

But to answer your question, the window.forms collection allows for indexing by the form name, which I was assuming to be 'x' -- I didn't realize that 'x' was a reference to the form and not the form name. My bad.

Jordan