473,395 Members | 1,969 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,395 software developers and data experts.

Elements[ ] index property

What is the syntax for finding out the element index number from a
specific element on a form?

For example, if you do
OnClick="document.forms[0].elements[12].checked=true", then when that
element is clicked, whatever the twelfth element is on the form,
assuming it is a checkbox or radio, will become checked. But what
about if you wanted to do the reverse - to click on an element and
have an alert box tell you that you had just clicked on elements[12]
of the form? How do I extract the index number property of that
element?

Hope someone can advise.

Steve Wylie
Jul 23 '05 #1
5 10657


Steve Wylie wrote:
What is the syntax for finding out the element index number from a
specific element on a form?
There is no "syntax" for that, nor is there a property or method defined
in the object model of current browsers as IE, Mozilla, or Opera.

But what
about if you wanted to do the reverse - to click on an element and
have an alert box tell you that you had just clicked on elements[12]
of the form? How do I extract the index number property of that
element?


You can write a function to do that
function getElementsIndex (element) {
if (element.form) {
for (var i = 0; i < element.form.elements.length; i++) {
if (element == element.form.elements[i]) {
return i;
}
}
return -1; // <input type="image"> in some browsers
}
else {
return -1;
}
}

then
<input onclick="var index = getElementsIndex(this);" ...>

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2
Lee
Steve Wylie said:

What is the syntax for finding out the element index number from a
specific element on a form?

For example, if you do
OnClick="document.forms[0].elements[12].checked=true", then when that
element is clicked, whatever the twelfth element is on the form,
assuming it is a checkbox or radio, will become checked. But what
about if you wanted to do the reverse - to click on an element and
have an alert box tell you that you had just clicked on elements[12]
of the form? How do I extract the index number property of that
element?


There's no simple syntax for that, because you almost never
need to know that. If you need a way to refer to a specific
element once you've found it, store a reference to it:

onclick="globalClickedElement=this"

If you really do need the number, then you loop through all
of the elements until you find the one that refers to the
same object as "this":

for(var i=0;i<document.forms[0].elements.length;i++){
if(this===document.forms[0].elements[i]){
alert("You clicked element number "+i);
break;
}
}

Jul 23 '05 #3
Thank you Martin, it works a treat. Just what I wanted. Also, the
website address you quote under your name, JavaScript.FAQTs.com, looks
very interesting.

Steve Wylie
Jul 23 '05 #4
Steve Wylie wrote:
For example, if you do
OnClick="document.forms[0].elements[12].checked=true", then when that
element is clicked, whatever the twelfth element is on the form,
assuming it is a checkbox or radio, will become checked.


No, it will happen to the thirteenth element (in order of appearance in
the source code).
PointedEars
Jul 23 '05 #5
Yes, it will too. Thank God it's only an example.

Steve
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Claude Schneegans | last post by:
Hi, In the code below, the lists elements at level 1 have a z-index of 1 and those at level 2 have it set to 2. Normaly, since level 1 and 2 overlap, level 2 elements should be seen above those...
3
by: Claude Schneegans | last post by:
Hi, The W3C says: "The 'z-index' property is used to specify the stacking order of positionable elements <#Positionable_Elements>." and it defines positionable elements as "Elements whose...
5
by: Denis Perelyubskiy | last post by:
Hello, I need to make an array of elements accross forms. My javascript skills, as evident from this question, are rather rudimentary. I tried to make an associative array and index it with...
4
by: Tad Marshall | last post by:
Hi, I'm reading about arrays in VB.NET and I seem to have a few options for my data structure. I need a multi-dimensional array of structures, and my first thought was Public Structure myStr...
24
by: RyanTaylor | last post by:
I have a final coming up later this week in my beginning Java class and my prof has decided to give us possible Javascript code we may have to write. Problem is, we didn't really cover JS and what...
6
by: pinetaj | last post by:
Hello, I have a question of using 'property' on accessing elements of array. There is an array member in a class. I'd like to restrict accessing the elements of the array through property. And...
3
by: Cylix | last post by:
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...
11
by: shankwheat | last post by:
I have a function which passes text from txtdebt to debtsbox which works fine. However, I want to add code which examines the value of debtsbox and if any of the values the user entered contain the...
0
by: Amar | last post by:
Hi, I have a generic list with a some objects of a particular class in it. I have implemented a IComparer for the the class and pass it to the List. The list.sort method works fine when the value...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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,...
0
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...
0
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...
0
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,...

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.