Connecting Tech Pros Worldwide Forums | Help | Site Map

Accessing elements of a form...

Pawe³
Guest
 
Posts: n/a
#1: Jul 20 '05
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="somefunction(this)">
and...
we have

function somefunction(obj) {


how can we obtain the N from within the function???

Michael Winter
Guest
 
Posts: n/a
#2: Jul 20 '05

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="somefunction(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(obj) {
>
> 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.elements[ N ]
will produce a reference to the Nth element of the INPUT element's
containing form.

Hope that helps,
Mike

--
Michael Winter
M.Winter@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Pawe³
Guest
 
Posts: n/a
#3: Jul 20 '05

re: Accessing elements of a form...


On Sat, 06 Mar 2004 12:37:29 GMT, in comp.lang.javascript 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="somefunction(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(obj) {
>>
>> 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.elements[ 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?

Michael Winter
Guest
 
Posts: n/a
#4: Jul 20 '05

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.elements, 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@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Pawe³
Guest
 
Posts: n/a
#5: Jul 20 '05

re: Accessing elements of a form...


[color=blue]
> function getIndex( obj ) {
> var e = obj.form.elements, 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 ;)
Closed Thread