[thelist] onFocus

.jeff jeff at members.evolt.org
Tue Oct 30 13:06:28 CST 2001


> From: Teck Low
> I would like to have the cursor focus in the input box.
> What is wrong with this statement?
> <form
>  name="srchfrm"
>  method="get"
>  action="engine.htm"
>  onFocus="window.document.srchfrm.srchtxt.focus();"
>  onSubmit="check(this.srchtxt);"
>  target="_top">
> <input
>  type="text"
>  size=30
>  name="srchtxt">

the <form> tag does not have an onfocus event handler.  you'll either need
to put a bit of script at the bottom of your page or call a function from
the onload event handler.

here's a function i use.  it will take any form and focus the first element
that can receive focus (provided you're not doing anything weird with
visibility, display, disabled, or readonly properties).

<script language="JavaScript" type="text/javascript">
  function focusForm()
    if(document.forms.length > 0)
      form = document.forms[0];
      for(var i = 0; i < form.elements.length; i++)
        if(form.elements[i].type != 'hidden')

window.onload = focusForm;
// -->

you're not, by chance, using the submit() method in your check() function
that you call with the onsubmit event handler?

good luck,


jeff at members.evolt.org

More information about the thelist mailing list