Accessing elements of a form...

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

    #1

    Accessing elements of a form...

    We can access the Nth element in the first form by

    document.forms[0].elements[N]

    imagine that theres function like
    <INPUT TYPE=TEXT" onclick="somefu nction(this)">
    and...
    we have

    function somefunction(ob j) {


    how can we obtain the N from within the function???
  • Michael Winter

    #2
    Re: Accessing elements of a form...

    On Sat, 6 Mar 2004 12:41:31 +0100, Paweł <pmg3@op.pl> wrote:
    [color=blue]
    > We can access the Nth element in the first form by
    >
    > document.forms[0].elements[N]
    >
    > imagine that theres function like
    > <INPUT TYPE=TEXT" onclick="somefu nction(this)">[/color]

    I assume that's a typo, and you meant: ... type="text" ... Note the
    difference in quotes.
    [color=blue]
    > and...
    > we have
    >
    > function somefunction(ob j) {
    >
    > how can we obtain the N from within the function???[/color]

    obj
    contains a reference to that INPUT element.
    obj.form
    will produce a reference to the INPUT element's containing form.
    obj.form.elemen ts[ N ]
    will produce a reference to the Nth element of the INPUT element's
    containing form.

    Hope that helps,
    Mike

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

    Comment

    • Pawe³

      #3
      Re: Accessing elements of a form...

      On Sat, 06 Mar 2004 12:37:29 GMT, in comp.lang.javas cript you wrote:
      [color=blue]
      > On Sat, 6 Mar 2004 12:41:31 +0100, Paweł <pmg3@op.pl> wrote:
      >[color=green]
      >> We can access the Nth element in the first form by
      >>
      >> document.forms[0].elements[N]
      >>
      >> imagine that theres function like
      >> <INPUT TYPE=TEXT" onclick="somefu nction(this)">[/color]
      >
      > I assume that's a typo, and you meant: ... type="text" ... Note the
      > difference in quotes.
      >[color=green]
      >> and...
      >> we have
      >>
      >> function somefunction(ob j) {
      >>
      >> how can we obtain the N from within the function???[/color]
      >
      > obj
      > contains a reference to that INPUT element.
      > obj.form
      > will produce a reference to the INPUT element's containing form.
      > obj.form.elemen ts[ N ]
      > will produce a reference to the Nth element of the INPUT element's
      > containing form.
      >
      > Hope that helps,[/color]
      Well it doesn't ...
      What I ask about is how do we know that Nth element is Nth??
      Let's say we have a row in the table, abd for each row we have rowIndex...
      I would like to have similiar property for Input element...
      Any ideas?

      Comment

      • Michael Winter

        #4
        Re: Accessing elements of a form...

        On Sat, 6 Mar 2004 20:16:18 +0100, Paweł <pmg3@op.pl> wrote:

        [snip]
        [color=blue]
        > What I ask about is how do we know that Nth element is Nth??
        > Let's say we have a row in the table, abd for each row we have
        > rowIndex...
        > I would like to have similiar property for Input element...[/color]

        You mean you want to know the index of a particular form control when you
        have a reference (and only a reference) to that control?

        There's no direct method. You'll have to loop through all controls in the
        form and check if the "mystery" reference matches a reference in the
        elements collection:

        function getIndex( obj ) {
        var e = obj.form.elemen ts, n = e.length;

        for( var i = 0; i < n; ++i )
        if( obj == e[ i ]) return i;
        return null;
        }
        ...
        <form ...>
        <input type="button" onclick="alert( getIndex(this)) ">
        <input type="button" onclick="alert( getIndex(this)) ">
        </form>

        Clicking on the first button will display 0. Clicking the second will
        display 1.

        Is that better?

        Mike

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

        Comment

        • Pawe³

          #5
          Re: Accessing elements of a form...

          [color=blue]
          > function getIndex( obj ) {
          > var e = obj.form.elemen ts, n = e.length;
          >
          > for( var i = 0; i < n; ++i )
          > if( obj == e[ i ]) return i;
          > return null;
          > }[/color]
          [color=blue]
          >
          > Is that better?[/color]

          Yeah, that's obviously better. Thank you, currently I'm working on a script
          to validate a form, and I would like my script to focus back on input field
          in case user enters an incorrect value. So now, I can just call the focus
          method on (N-1)th element and that should be fine I hope ;)

          Comment

          Working...