Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

Q1143 How can I get the elements index number of a form field using 'this'?

irt.org | Knowledge Base | JavaScript | Form 3.1 | Q1143 [ previous next ]

Q1143 How can I get the elements index number of a form field using 'this'?

Try:

<script language="JavaScript"><!--
function getIndex(what) {
    for (var i=0;i<document.myForm.elements.length;i++)
        if (what == document.myForm.elements[i])
            return i;
    return -1;
}

function test(what) {
    alert(getIndex(what));
}
//--></script>

<form name="myForm">
<Input type="button" value="Button0" onClick="test(this)">
<input type="button" value="Button1" onClick="test(this)">
<input type="button" value="Button2" onClick="test(this)">
<input type="button" value="Button3" onClick="test(this)">
</form>

Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 30th March 2008. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2008 irt.org, All Rights Reserved.