Forms array peculiarities

B

Bonnett

I have a form which contains quantity and subtotal input boxes for
ordering things, i use javascript to calculate the value to be placed
in the subtotal box by iterating through a document.myForm[] array
(where myForm is the name of the form). The indexing I use works fine
in IE6 and Firefox 0.10.1, however when testing in NN6, i noticed that
the myForm[] array was also including <label> elements, whereas in ie6
and ff it only includes the input boxes. I have managed to detect when
this occurs and change the index for when this occurs, however I would
like to know whether any other browsers have quirks like this, I don't
particularly want to have to install IE4 or 5.5 etc...

Which is more compatible:
document.myForm.item1Quantity.value or
document.myForm[0].value
for referencing form elements?
 
M

Martin Honnen

Bonnett said:
I have a form which contains quantity and subtotal input boxes for
ordering things, i use javascript to calculate the value to be placed
in the subtotal box by iterating through a document.myForm[] array
(where myForm is the name of the form). The indexing I use works fine
in IE6 and Firefox 0.10.1, however when testing in NN6, i noticed that
the myForm[] array was also including <label> elements, whereas in ie6
and ff it only includes the input boxes. I have managed to detect when
this occurs and change the index for when this occurs, however I would
like to know whether any other browsers have quirks like this, I don't
particularly want to have to install IE4 or 5.5 etc...

In current browsers like IE 6, Netscape 7, Opera 7 <fieldset> elements
are also contained in the elements collection of a said:
Which is more compatible:
document.myForm.item1Quantity.value or
document.myForm[0].value
for referencing form elements?

Use
document.forms.myForm.elements
to loop through, then check tagName of the element you are acessing.
 
G

Grant Wagner

Bonnett said:
I have a form which contains quantity and subtotal input boxes for
ordering things, i use javascript to calculate the value to be placed
in the subtotal box by iterating through a document.myForm[] array
(where myForm is the name of the form). The indexing I use works fine
in IE6 and Firefox 0.10.1, however when testing in NN6, i noticed that
the myForm[] array was also including <label> elements, whereas in ie6
and ff it only includes the input boxes. I have managed to detect when
this occurs and change the index for when this occurs, however I would
like to know whether any other browsers have quirks like this, I don't
particularly want to have to install IE4 or 5.5 etc...

Which is more compatible:
document.myForm.item1Quantity.value or
document.myForm[0].value
for referencing form elements?

document.forms['myFormName'].elements['myControlName'].value

should work in any user agent that understands forms and client-side
JavaScript. Using bracket notation on the collections also avoids any
problems when you begin to work with scripts that can read input from a
variety of inputs, decided at run-time:

var myControlNameVariable = 'myControl' + 'Name';
document.forms['myFormName'].elements[myControlNameVariable].value;

All of this depends on your form having the general layout:

<form name="myFormName" id="myFormId" ...>
<input type="text" name="myControlName" id="myControlId" ...>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top