Click to See Complete Forum and Search --> : for loop question


Wallykid
06-30-2003, 11:57 AM
for(var i=1; i<=10;i++)
{
if (document.form1.att_name_+i.value == "")
{
count = document.form1.regular_lunch.value + document.form1.veggie_lunch.value;
if (count != (i-1))
{
confirm("The total number of attendees does not equal to the total number of lunches");
}
}
}

if (document.form1.att_name_+i.value == "")

This is the part where i'm not sure. I have a series of textbox with att_name_1, att_name_2 and so on...i am trying to use the loop counter i to substitute the the numbering part of the text box. Is this way the right way to do it?

Charles
06-30-2003, 12:01 PM
You can either use the form's "elements" Array or make use of the fact that in JavaScript objects are also associative arrays. Id, est: document.form1['att_name_' + i].value.

Wallykid
06-30-2003, 12:25 PM
Originally posted by Charles
You can either use the form's "elements" Array or make use of the fact that in JavaScript objects are also associative arrays. Id, est: document.form1['att_name_' + i].value.

i assume the way code you have up there is the Javascript objects as associative arrays. How does the form's elements" array method works?

BTW, it is already working with the way you suggested with the code. Thanks.

Charles
06-30-2003, 12:36 PM
Originally posted by Wallykid
How does the form's elements" array method works? You don't need to use names at all. The FORMs on a page are contained in an Array and are numbered from 0. And the elements in each FORM are also contained in an Array and numbered from 0. The get the value of the second element of the first form of the current document you could use: document.forms[0].elements[1].value

Wallykid
06-30-2003, 03:07 PM
Thanks for all the answers.

Wallykid
07-02-2003, 10:16 AM
If the fact that in JavaScript objects are also associative arrays. Id, est: document.form1['att_name_' + i].value.

wouldn't document.form1['att_name' + i].focus(); work?

i in this case is being declared in the for loop, and is not in the begginning of the function, does that mean i can't use it again after the for loop?

for(var i=1; i<=10;i++)
{
if (document.form1.att_name_+i.value == "")
{
count = document.form1.regular_lunch.value + document.form1.veggie_lunch.value;
if (count != (i-1))
{
confirm("The total number of attendees does not equal to the total number of lunches");
}
}
}

if (document.form1.att_name_+i.value == "")