Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jquery

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?

like image 557
Jimmy Avatar asked Oct 07 '09 18:10

Jimmy


People also ask

How to set focus on form input text field on page load?

| Set focus on a form input text field on page load. This input box gets the focus as the page loads. Example 2: In this example the form input text field gets the focus as page loads by using focus () method . Here the input element is selected by id of form element and its own id in JQuery selector. input text field on page load.

How to auto-focus the input field on page reload?

Create a simple form on which we set the input field as focused on page reload. So we have created a signup form with two input fields “email” and “password” and add the directive “autofocus” to the “email” input field and then this field is auto-focus on page reload.

How to focus on an element using jQuery?

The focus() is an inbuilt method in jQuery which is used to focus on an element. The element get focused by the mouse click or by the tab-navigating button. Syntax: $(selector).focus(function) Here selector is the selected element. Parameter: It accepts an optional parameter “function” which specifies the function to run when the focus ...

How to set input field as focused on page reload in angular?

Approach : 1 Create the Angular app to be used. 2 Create a directive “AutoFocus” that handles the auto-focus of input fields. In this directive set the HTML element as focused. 3 Then import this directive inside app.module.ts and add it to the provider’s list. 4 Create a simple form on which we set the input field as focused on page reload. ...


2 Answers

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>
like image 142
Mike Blandford Avatar answered Dec 21 '22 07:12

Mike Blandford


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();
like image 35
John Sheehan Avatar answered Dec 21 '22 07:12

John Sheehan