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

Q246 The onLoad event handler does not always set the focus in a form field, are there any workarounds?

You are here: irt.org | FAQ | JavaScript | Form | 2 | Q246 [ previous next ]

The following does not always work:

<BODY onLoad="document.forms[0].elements[0].focus()">

<FORM>
<INPUT TYPE="TEXT" VALUE="1234">
</FORM>

Try either the following:

<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript"><!--
function setFocus() {
    document.forms[0].elements[0].focus();
}
//--></SCRIPT>
</HEAD>

<BODY onLoad="setTimeout('setFocus()',1)">

<FORM>
<INPUT TYPE="TEXT" VALUE="1234">
</FORM>

</BODY>
</HTML>

Or:

<FORM>
<INPUT TYPE="TEXT" VALUE="1234">
</FORM>

<SCRIPT LANGUAGE="JavaScript"><!--
document.forms[0].elements[0].focus();
//--></SCRIPT>

©2018 Martin Webb