View Full Version : How do I get a form element to notify me of its element number within a form???

02-16-2009, 10:42 AM
I can find an element using a loop to check through all the elements within a form but I need a way that an element (eg a checkbox which is one of many of the same name) can notify me of its array position when i click it.

The checkbox is defined within 'myform' as follows in a php loop which builds up the form:
<input type='checkbox' name='addme' id='addme' onClick='addthis()'>

in this way I create a form of club members with a checkbox whcih I intend to use to add/delete members to a mail list.

I can get addthis() to activate with general debug messages but it will not report document.myform.addme.length. Just one of my issues as I dont know how many elements (ie club members in the form) to loop through.

I want to click on any checkbox and use its array position to set another array element with a corresponding value.
Any suggestion gratefully accepted - i am a newbie after all...

02-16-2009, 01:04 PM
<script language="JavaScript" type="text/JavaScript">
function showIndex(field) {
var form = document.blah;
for(i=0;i<form.elements.length;i++) {
if(form.elements[i].id == field) {
alert("The index of this field is " + i);


<form name="blah">
<input type="text" name="fieldname" id="field1" onclick="showIndex(this.id);"><br>
<input type="text" name="fieldname" id="field2" onclick="showIndex(this.id);"><br>
<input type="text" name="fieldname" id="field3" onclick="showIndex(this.id);"><br>

Names can be the same (and should be in case of checkboxes and radio buttons) but id's should be unique. I hope this gets you any further!