An element named 'length'

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

    An element named 'length'

    Hello, I'm having problems on IE 6.0 for Win XP. I want to iterate
    through the elements of a form, but one of those elements is named
    'length'. So, this JS code doesn't display any alert boxes, even
    though it should display 3.

    for (var i=0; i<myForm.elemen ts.length; i++) {
    alert(myForm.el ements[i].name);
    }

    Can anyone suggest a cross-browser work-around? Thanks -
  • Michael Winter

    #2
    Re: An element named 'length'

    On 2 Feb 2004 10:32:29 -0800, D. Alvarado <laredotornado@ zipmail.com>
    wrote:
    [color=blue]
    > Hello, I'm having problems on IE 6.0 for Win XP. I want to iterate
    > through the elements of a form, but one of those elements is named
    > 'length'. So, this JS code doesn't display any alert boxes, even
    > though it should display 3.
    >
    > for (var i=0; i<myForm.elemen ts.length; i++) {
    > alert(myForm.el ements[i].name);
    > }
    >
    > Can anyone suggest a cross-browser work-around? Thanks -[/color]

    Use FormObject.leng th instead.

    In future, don't use names that might conflict with object properties and
    methods.

    Mike

    --
    Michael Winter
    M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

    Comment

    • Brian Genisio

      #3
      Re: An element named 'length'

      D. Alvarado wrote:
      [color=blue]
      > Hello, I'm having problems on IE 6.0 for Win XP. I want to iterate
      > through the elements of a form, but one of those elements is named
      > 'length'. So, this JS code doesn't display any alert boxes, even
      > though it should display 3.
      >
      > for (var i=0; i<myForm.elemen ts.length; i++) {
      > alert(myForm.el ements[i].name);
      > }
      >
      > Can anyone suggest a cross-browser work-around? Thanks -[/color]

      Hmmmm...

      Option #1:
      Rename the offending element to something else (such as len)

      Opeiont #2:

      for(var i in myForm.elements )
      {
      alert(myForm.el ements[i].name);
      }

      Brian

      Comment

      • Richard Cornford

        #4
        Re: An element named 'length'

        "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message
        news:opr2raezg8 5vklcq@news-text.blueyonder .co.uk...
        <snip>[color=blue]
        >Use FormObject.leng th instead.[/color]

        That probably won't help as providing the shortcut notation of allowing
        named form controls to be accessed as named members of the form object
        will mean that the - length - property of the form will have been
        replaced with a reference to the same form control.

        On DOM browsers:-

        var inputs = formObject.getE lementsByTagNam e('input');
        var selects = formObject.getE lementsByTagNam e('select');

        - and iterating over the resulting nodeList objects. But there has got
        to be a better way.
        [color=blue]
        >In future, don't use names that might conflict with
        >object properties and methods.[/color]

        And that is it. :)

        Richard.


        Comment

        • Michael Winter

          #5
          Re: An element named 'length'

          On Mon, 2 Feb 2004 19:10:32 -0000, Richard Cornford
          <Richard@litote s.demon.co.uk> wrote:
          [color=blue]
          > "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message
          > news:opr2raezg8 5vklcq@news-text.blueyonder .co.uk...
          >[color=green]
          >> Use FormObject.leng th instead.[/color]
          >
          > That probably won't help as providing the shortcut notation of allowing
          > named form controls to be accessed as named members of the form object
          > will mean that the - length - property of the form will have been
          > replaced with a reference to the same form control.[/color]

          I seem to be overlooking a lot of details today...

          Thank you. I'd also like to thank you for your advice on DOM feature
          detection given last week (23rd).

          Mike

          --
          Michael Winter
          M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

          Comment

          • Richard Cornford

            #6
            Re: An element named 'length'

            "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message
            news:opr2rb25au 5vklcq@news-text.blueyonder .co.uk...
            <snip>[color=blue]
            >I seem to be overlooking a lot of details today...[/color]

            So am I. I forgot about textarea elements.
            [color=blue]
            >... . I'd also like to thank you for your advice on
            >DOM feature detection ...[/color]

            You are welcome. It is probably one of the most important aspects of
            matching a script with a browser's possible support for it and deserves
            talking about from time to time.

            Richard.


            Comment

            • Michael Winter

              #7
              Re: An element named 'length'

              On Mon, 2 Feb 2004 23:00:38 -0000, Richard Cornford
              <Richard@litote s.demon.co.uk> wrote:
              [color=blue]
              > "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message
              > news:opr2rb25au 5vklcq@news-text.blueyonder .co.uk...
              >[color=green]
              >> I seem to be overlooking a lot of details today...[/color]
              >
              > So am I. I forgot about textarea elements.[/color]

              I actually noticed that. :) For thoroughness, you could also mention
              BUTTON elements, but they wouldn't be accessed nearly as much.

              Mike

              --
              Michael Winter
              M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

              Comment

              • John

                #8
                Re: An element named 'length'

                laredotornado@z ipmail.com (D. Alvarado) wrote in
                news:9fe1f2ad.0 402021032.1cff8 be7@posting.goo gle.com:
                [color=blue]
                > Hello, I'm having problems on IE 6.0 for Win XP. I want to iterate
                > through the elements of a form, but one of those elements is named
                > 'length'. So, this JS code doesn't display any alert boxes, even
                > though it should display 3.
                >
                > for (var i=0; i<myForm.elemen ts.length; i++) {
                > alert(myForm.el ements[i].name);
                > }
                >
                > Can anyone suggest a cross-browser work-around? Thanks -
                >[/color]

                Umm, am I missing something? Why not just change the name of the element
                named 'length'?

                John

                Comment

                • D. Alvarado

                  #9
                  Re: An element named 'length'

                  John <john@mshome.ne t> wrote in message news:<Xns948F16 FCEE2D5johnmsho menet@203.26.24 .228>...[color=blue]
                  > laredotornado@z ipmail.com (D. Alvarado) wrote in
                  > news:9fe1f2ad.0 402021032.1cff8 be7@posting.goo gle.com:
                  >
                  > Umm, am I missing something? Why not just change the name of the element
                  > named 'length'?
                  >
                  > John[/color]

                  Normally a good idea. I do not have control over the variable name --
                  it is required by a third party site to which I'm passing data.

                  Thanks to all for the helpful answers. -

                  Comment

                  Working...