Jquery - Focus on an input field in a form on pageload?

jquery

Solution

It does work in the following simple example. Therefore there is something else going on on your page that causes the input to lose focus. I suggest using setTimeout to set the focus.

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <title>test!</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js">
    </script>
    <script type="text/javascript">
      $(document).ready(function() {
    $('#Username').focus();
});
    </script>

  </head>

  <body>
  <input id="Username" />
  </body>
</html>

Problem

I am trying to focus on the username input in my login screen to allow for easier access to logging in and currently my jquery looks like this ``` $(document).ready(function() { $('#Username').focus(); }); ``` but that doesn't work... any ideas?

Original source