form's field number

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

    #1

    form's field number

    Input field of the form. How to check which number this field has ? I mean
    the order number, like variable elementNumber in
    document.forms[formnumber].elements[elementNumber] ? Is it possible to check
    without looping and comparing some attributes in every loop ?

    Best regards,
    ABS


  • RobB

    #2
    Re: form's field number

    abs wrote:[color=blue]
    > Input field of the form. How to check which number this field has ? I[/color]
    mean[color=blue]
    > the order number, like variable elementNumber in
    > document.forms[formnumber].elements[elementNumber] ? Is it possible[/color]
    to check[color=blue]
    > without looping and comparing some attributes in every loop ?
    >
    > Best regards,
    > ABS[/color]

    Why do you need this? Just curious. The only way afaik to do it without
    run-time looping is to use a script onload that sets an index property
    on each element:

    window.onload = function()
    {
    var f, els, el, i = 0, j;
    while (f = document.forms[i++])
    {
    els = f.elements;
    j = 0;
    while (el = els[j])
    el.index = j++;
    }
    }

    Then access as elementObj.inde x.

    Comment

    • abs

      #3
      Re: form's field number

      RobB wrote:[color=blue]
      > Why do you need this? Just curious.[/color]

      Because I can't use id and I need to identify the field.

      Thank you for your response.

      Regards,
      ABS


      Comment

      • abs

        #4
        Re: form's field number

        RobB wrote:[color=blue]
        > The only way afaik to do it
        > without run-time looping is to use a script onload that sets an index
        > property on each element:[/color]

        One more question. maybe you know hot to get the element having specific
        index ? Something like getElementByInd ex() ?

        ABS


        Comment

        • RobB

          #5
          Re: form's field number

          abs wrote:[color=blue]
          > RobB wrote:[color=green]
          > > The only way afaik to do it
          > > without run-time looping is to use a script onload that sets an[/color][/color]
          index[color=blue][color=green]
          > > property on each element:[/color]
          >
          > One more question. maybe you know hot to get the element having[/color]
          specific[color=blue]
          > index ? Something like getElementByInd ex() ?
          >
          > ABS[/color]

          Hmm...that's why I asked what you needed here. First you had the
          element object & wanted its position in the elements[] collection. Now
          you've got the position and want the object.

          FormObj.element s[index];

          There are other ways to obtain a valid object reference to a form field
          (or any HTML element) that rely upon DOM - document structure - rather
          than ids or names. It would help to know what you were trying to
          accomplish.

          cheers, Rob

          Comment

          • abs

            #6
            Re: form's field number

            RobB wrote:[color=blue]
            > Hmm...that's why I asked what you needed here. First you had the
            > element object & wanted its position in the elements[] collection. Now
            > you've got the position and want the object.[/color]

            That's how it is :) It may seem stupid but I really need this.
            [color=blue]
            > There are other ways to obtain a valid object reference to a form
            > field (or any HTML element) that rely upon DOM - document structure -
            > rather than ids or names.[/color]

            OK, I invented something like that: I'll set the fields' indexes as in the
            pattern:
            field.index = numberOfForm_nu mberOfField
            And than, when i'll need to acces the filed, I'll split its index and use
            document.forms[numberOfForm].elements[numberOfField].
            Hope it will work :)

            Regards,
            ABS


            Comment

            • Lee

              #7
              Re: form's field number

              abs said:[color=blue]
              >
              >RobB wrote:[color=green]
              >> Why do you need this? Just curious.[/color]
              >
              >Because I can't use id and I need to identify the field.[/color]

              Why not use the field's "name" attribute?

              Comment

              • abs

                #8
                Re: form's field number

                Lee wrote:[color=blue]
                > Why not use the field's "name" attribute?[/color]

                Because I've got a form with names like 'date[]', 'something[]', etc and
                there might be a lot of fields with the same name. It's not my will, so it
                has to be like that.

                ABS


                Comment

                • Lee

                  #9
                  Re: form's field number

                  abs said:[color=blue]
                  >
                  >Lee wrote:[color=green]
                  >> Why not use the field's "name" attribute?[/color]
                  >
                  >Because I've got a form with names like 'date[]', 'something[]', etc and
                  >there might be a lot of fields with the same name. It's not my will, so it
                  >has to be like that.[/color]

                  So why not use the field's "name" attribute?

                  document.forms[0].elements["date[]"].value

                  Comment

                  • abs

                    #10
                    Re: form's field number


                    U¿ytkownik "Lee" <REM0VElbspamtr ap@cox.net> napisa³ w wiadomo¶ci
                    news:d4jpr70mbi @drn.newsguy.co m...[color=blue]
                    > So why not use the field's "name" attribute?
                    > document.forms[0].elements["date[]"].value[/color]

                    As I wrote, the fields might have the same names, like:

                    <input type="text" name="myname[]">
                    <input type="text" name="myname[]">

                    Regards,
                    ABS


                    Comment

                    • RobB

                      #11
                      Re: form's field number

                      abs wrote:[color=blue]
                      > U¿ytkownik "Lee" <REM0VElbspamtr ap@cox.net> napisa³ w wiadomo¶ci
                      > news:d4jpr70mbi @drn.newsguy.co m...[color=green]
                      > > So why not use the field's "name" attribute?
                      > > c[/color]
                      >
                      > As I wrote, the fields might have the same names, like:
                      >
                      > <input type="text" name="myname[]">
                      > <input type="text" name="myname[]">
                      >
                      > Regards,
                      > ABS[/color]

                      <input type="text" name="myname[]">
                      <input type="text" name="myname[]">
                      ..............
                      ..............
                      document.forms[0].elements["myname[]"][0]
                      document.forms[0].elements["myname[]"][1]

                      Comment

                      • Thomas 'PointedEars' Lahn

                        #12
                        Re: form's field number

                        abs wrote:
                        [color=blue]
                        > U¿ytkownik "Lee" <REM0VElbspamtr ap@cox.net> napisa³ [...][color=green]
                        >> So why not use the field's "name" attribute?
                        >> document.forms[0].elements["date[]"].value[/color]
                        >
                        > As I wrote, the fields might have the same names, like:
                        >
                        > <input type="text" name="myname[]">
                        > <input type="text" name="myname[]">[/color]

                        I still don't understand why you need the index. On event, pass the `this'
                        reference or use `event.target || event.srcElemen t' and you don't need any
                        index, let alone the element's name.


                        HTH

                        PointedEars
                        --
                        Multiple exclamation marks are a sure sign of a diseased mind.
                        -- Terry Pratchett

                        Comment

                        Working...