473,385 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to know the form elements index?

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
3 2399

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Eddie | last post by:
I have a form that's used to sort a series of items. The form has a number of text fields. Each text field should contain a number. When the form is submitted I would like to do two things: ...
2
by: Citoyen du Monde | last post by:
Trying to get some ideas on a simple javascript project (to teach myself the language). I want to develop a client-side vocabulary practice application that would allow users to enter their own...
18
by: Michal Mieszkowski | last post by:
i have a block of html code looking like this <FORM name=myform> <INPUT type=text name=firstname> <DIV id=mydiv> <INPUT type=text name=address> </DIV> </FORM> i can access firstname field...
1
by: Phil Powell | last post by:
function populateWithPics(folder) { if (folder.value != '') { monthIndex = folder.name.substring(folder.name.indexOf("'") + 1, folder.name.indexOf('\]') - 1); indexArray =...
5
by: Craig Anderson | last post by:
Can anyone tell me the best way to access a hidden object in a form? I could use a hard-coded index to the elements of the form, but it's too easy to add something before the hidden object and mess...
6
by: WindAndWaves | last post by:
Hi Gurus In my quest in putting my first javascript together, I am now trying to conquer something that seems trivial, but has taken me hours. I would like to format a field in a form once the...
2
by: justplain.kzn | last post by:
Hi, I have a table with dynamic html that contains drop down select lists and readonly text boxes. Dynamic calculations are done on change of a value in one of the drop down select lists. ...
9
by: dhtml | last post by:
I have written an article "Unsafe Names for HTML Form Controls". <URL: http://jibbering.com/faq/names/ > I would appreciate any reviews, technical or otherwise. Garrett --...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.