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

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

irt.org | Knowledge Base | JavaScript | Form 2 | Q246 [ previous next ]

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

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>

Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


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