HTML autofocus attribute

HTML autofocus attribute automatically focuses the cursor on the specific input field after the page is loaded.

<form action="files/autofocus.php" method="post">
<p>Email: <input type="email" name="email" autofocus></p>
<p>Password: <input type="password" name="password"></p>
<p><input type="submit" name="login"></p>
</form>
</>

Email:

Password:

autofocus attribute allows the user to start typing directly in the respective input area.

In the above example, the input control of email is focused after the page is loaded.

Attribute values

All possible values of autofocus attribute

autofocus is a boolean attribute. A boolean attribute has only two states.

Related Tags

The tags that support autofocus attribute

<button> tag

The presence of autofocus attribute focuses the button automatically.

<button type="button" autofocus>Submit</button>
Try</>

<input> tag

input control is focused automatically after the page is loaded.

<p> User name: <input type="text" name="uname" placeholder="Name" autofocus></p><p> User name: <input type="password" name="pass" ></p>
Try</>

<textarea> tag

the textarea element also supports autofocus attribute.

<textarea cols="25" rows="9" autofocus>
 It is textarea. It is editable and resizeable by default.But the size can be fixed. The textarea is focused by autofocus attribute.
 </textarea>
Try</>


Was this article helpful?

 

Email:

Message: