Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assign form field values to array elements using a loop 2

Status
Not open for further replies.

LOOP123

Programmer
Mar 7, 2008
10
CA
Hello,

I have the following javascript code that I want to shorten using a loop that will assign form field values to array elements:

var N = new Array();

N[0] = frmOpt1.opt1N0.value;
N[1] = frmOpt1.opt1N1.value;
N[2] = frmOpt1.opt1N2.value;
N[3] = frmOpt1.opt1N3.value;
N[4] = frmOpt1.opt1N4.value;[/color blue]

Here's what I've come up with, however, it's not working out for me:

var N = new Array();

for (var x = 0; x < 5; x++){
N[x] = frmOpt1.["opt1N" + x].value;
}[/color red]

Any help would be greatly appreciated - I'm a newbie :)

Thanks in advance :)
 
You need to use the elements collection (and while you're at it, you should be using the forms collection as well). Just because something works in one browser it doesn't mean it will work in another. For that reason, you should use proper syntax every time, even if it means more typing:

Code:
var N = new Array();

for (var x = 0; x < 5; x++) {
   N[x] = [!]document.forms[[/!]"frmOpt1"[!]][/!].[!]elements[[/!]"opt1N" + x[!]][/!].value;
}

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
Thanks - however, for some reason, I'm now getting an error. At first, I thought it may be due to a missing . between forms["frmOpt1"][/color blue] as follows:

var N = new Array();

for (var x = 0; x < 5; x++) {
N[x] = document.forms.["frmOpt1"].elements["opt1N" + x].value;
}
[/color blue]

I have confirmed the id of my form is frmOpt1 - I just can't figure out why I'm getting an error.

Thanks in advance!
 
Wow, for once I post a copy/paste example, and the recipient didn't actually copy/paste [shocked]

Usually it's the other way around. I post a pseudocode example and they complain when they copy/paste it and it doesn't work [lol]

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
I get this error when I omit the "." between
forms["frmOpt1"][/color blue]:

Error (in IE):
document.forms.["frmOpt1"].elements[...].value' is null or not an object

***

LOL - copy and paste is good, however, I'm trying to master this stuff :)

fyi...I really want to thank everyone for their assistance. Kudos to everyone that helps poor saps like me out.
 
I'd guess that your code is trying to access an element that doesn't exist. Try using alert(x) before N[x] = document.forms.["frmOpt1"].elements["opt1N" + x].value;. The last x you see will be the one that's causing the error.

And it's always good to copy and paste your revised code so we can see what you have after modifications.

Lee
 
LOL - copy and paste is good, however, I'm trying to master this stuff :)

That's good. You did the right thing by typing it out yourself. You'll definitely learn more that way. It's just not the route that most posters take, unfortunately.

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top