tags:

views:

104

answers:

4

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?

+3  A: 

Does the tag have an id attribute?

<input id="Username" name="Username" type="text" />

I'm guessing it only has a name attribute:

<input name="Username" type="text" />

If you can't add the ID attribute, you can select it like this:

$("input[name='Username']").focus();
John Sheehan
A: 

Your HTML looks like <input id="Username" ...> (Case-sensitive)?

Dark Falcon
A: 

Well, that is how you do it. Make sure the ID is correct, I suppose.

chaos
A: 

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"&gt;
<html>
  <head>
    <title>test!</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;
    </script>
    <script type="text/javascript">
      $(document).ready(function() {
    $('#Username').focus();
});
    </script>

  </head>

  <body>
  <input id="Username" />
  </body>
</html>
Mike Blandford
not sure why it isn't working... not sure whats going on
Jimmy