By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,274 Members | 2,229 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,274 IT Pros & Developers. It's quick & easy.

How to know the form elements index?

P: n/a
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.

Jul 24 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a

Cylix wrote:
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.
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.

Jul 24 '06 #2

P: n/a
Actually, I need the index to get the next element ...

web.dev wrote:
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.
Jul 24 '06 #3

P: n/a
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);" >
Cylix wrote:
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.
Jul 24 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.