473,654 Members | 3,289 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2410

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="getInd ex(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
4581
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: 1. Verify that only numbers are entered in each text field. 2. Verify that there are no duplicate entries in any text field. So each field should be a unique number. Another thing complicating this is that I do not know how many fields I will...
2
3077
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 words, their own definitions plus an example of how the word is used in practice. It'll be all client side with - cookies? to get persistence so that the words won't disappear on me each time the page is closed (which is what happened when I
18
7239
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 with document.myform.firstname
1
1156
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 = folder.value.split('|'); index = indexArray; if (!isNaN(index)) { for (i = 0; i < picsArray.length; i++) { document.calendarPicForm.elements'].options.value = picsArray; document.calendarPicForm.elements'].options.text = picsArray;
5
2440
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 up the indexing. For example: The form has a tagid of "myForm" The hidden object has a tagId of "myHiddenObj"
6
9626
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 person has completed it. The format should be "00". For example, if the person puts 1 then it should become 01 and if the person puts 12 in the field then it should stay like that. If the person puts 2004 then it should become 04, and if the...
2
2108
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. Using Safari,my first iteration the script works fine ( indicating that there are 33 form variables ). When trying another dropdown select value, the
9
2504
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 -- comp.lang.javascript FAQ <URL: http://jibbering.com/faq/ >
0
8376
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8815
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8708
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8489
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7307
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5622
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1596
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.