Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

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

You are here: irt.org | FAQ | JavaScript | Form | 3.1 | Q1143 [ previous next ]

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>

©2018 Martin Webb