Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to know the form elements index?

0 views
Skip to first unread message

Cylix

unread,
Jul 23, 2006, 11:12:34 PM7/23/06
to
I have a form that contains around 20 input elements,
they have some onclick event on it.

I would like to know how can I get the form element index when the
onclick event fire.
I found no property or method to do so.

Thank you.

web.dev

unread,
Jul 24, 2006, 1:27:59 AM7/24/06
to

If you pass 'this' to your event handler function, then you no longer
need to know the index for your form. Because 'this' will become a
reference to your form element that you've clicked on.

For more information, do a search for the javascript keyword this.

Cylix

unread,
Jul 24, 2006, 3:27:23 AM7/24/06
to
Actually, I need the index to get the next element ...

Noah Sussman

unread,
Jul 24, 2006, 6:09:44 AM7/24/06
to
> I would like to know how can I get the form element index when the
> onclick event fire.

When the element is clicked, iterate through the form elements until
you find that element's ID. If you don't want to use ID, you could use
any other unique property value.

function getIndex(formEl) {
for( var x=0; x< myForm.length; x++) {
if (myForm[x].id == formEl.id) alert( "The index of this element
is " + x );
}
}


Then your HTML would be something like:

< input id="foo" onclick="getIndex(this);" >

0 new messages