Forms array peculiarities

Discussion in 'Javascript' started by Bonnett, Nov 2, 2004.

  1. Bonnett

    Bonnett Guest

    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?
    Bonnett, Nov 2, 2004
    #1
    1. Advertising

  2. Bonnett wrote:

    > 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 <form> element.

    > 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.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Nov 2, 2004
    #2
    1. Advertising

  3. Bonnett

    Grant Wagner Guest

    Bonnett wrote:

    > 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" ...>

    --
    Grant Wagner <>
    comp.lang.javascript FAQ - http://jibbering.com/faq
    Grant Wagner, Nov 2, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brendan Miller

    Web Forms VS Windows Forms

    Brendan Miller, Aug 11, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    5,056
    Kevin Spencer
    Aug 11, 2003
  2. Eric
    Replies:
    2
    Views:
    1,405
    Tommy
    Feb 13, 2004
  3. jlopes151
    Replies:
    2
    Views:
    608
    Steve C. Orr [MVP, MCSD]
    Mar 14, 2005
  4. Mamluk Caliph

    Pre-processor peculiarities

    Mamluk Caliph, May 26, 2008, in forum: C Programming
    Replies:
    1
    Views:
    225
    Mamluk Caliph
    May 26, 2008
  5. Eric
    Replies:
    2
    Views:
    467
Loading...

Share This Page