Click to See Complete Forum and Search --> : JavaScript - Referencing Form elements


aahannaik
12-06-2003, 09:07 PM
Hi, I've a Form that have various columns called stocklondon, stockny,stockjapan......etc etc. Since there are multiple rows for each columns, their names are suffixed with 1,2,3,.... and so on. This number is pretty much dynamic.

The form has columns with form elements as :

stocklondon1 stockny1 stockjapan1
stocklondon2 stockny2 stockjapan2
stocklondon3 stockny3 stockjapan3
stocklondon4 stockny4 stockjapan4
: : :
: : :

Their totalnumber is unknown. I want to sum the row quantities, i.e. all 1's, 2's, 3's..... and so on. I want to make a loop like this :

for (int i=0;i<count;i++)
{
document.form.total[i].value=document.form.stocklondon[i].value + document.form.stockny[i].value + document.form.stockjapan[i].value;
}

However, it does not recognize this as a form element and gives an error. How can I accomplish this ??? ( without using form[0].elements, since I want values of 1 summed to total of 1 )

Thanks.

Khalid Ali
12-06-2003, 11:15 PM
use DOM methods
getElementsByName()
or
getELementsBYId()

or getElementsBYTagName()

aahannaik
12-07-2003, 06:58 AM
Never Mind. I got it using eval()

woodknut
12-08-2003, 11:06 AM
Khalid,
I hate to highjack the thread, but since aahannaik has answered his own question...

How would you reference a form element called "product1[]" by using DOM methods? More specifically how would you check the values of this element if this element is an "array" of values?

I recall getting an undefined/null for elementItem[0] error when doing this:

elementItem = getElementById('product1[]');
if ( elementItem[0].value == '' )
yadaYada();

The reason I ask is that I'm submitting form elements to a shopping cart that requires the '[]' on this element name.

WoodK.