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?
| 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.
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.
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 ...
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. ...
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>
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With