Forms array peculiarities

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bonnett

    Forms array peculiarities

    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?
  • Martin Honnen

    #2
    Re: Forms array peculiarities



    Bonnett wrote:
    [color=blue]
    > 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...[/color]

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

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

    --

    Martin Honnen

    Comment

    • Grant Wagner

      #3
      Re: Forms array peculiarities

      Bonnett wrote:
      [color=blue]
      > 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?[/color]

      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 myControlNameVa riable = 'myControl' + 'Name';
      document.forms['myFormName'].elements[myControlNameVa riable].value;

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

      <form name="myFormNam e" id="myFormId" ...>
      <input type="text" name="myControl Name" id="myControlId " ...>

      --
      Grant Wagner <gwagner@agrico reunited.com>
      comp.lang.javas cript FAQ - http://jibbering.com/faq

      Comment

      Working...