HTML autofocus Attribute


The autofocus attribute of the <input> element is used to specify which of the <input> would get focus when the web page loads. This attribute introduced in HTML5.

Following is the syntax −

<input autofocus>

Let us now see an example to implement the autofocus attribute of the <input> element −

Example

 Live Demo

<!DOCTYPE html>
<html>
<body>
<h2>Log in to your account</h2>
<form action="" method="get">
   Id: <input type="text" name="id"><br>
   Password: <input type="password" name="pwd"><br>
   DOB: <input type="date" name="dob" autofocus><br>
   <button type="submit" value="Submit">Submit</button>
</form>
</body>
</html>

Output

In the above example, we have created a form −

<form action="" method="get">
   Id: <input type="text" name="id"><br>
   Password: <input type="password" name="pwd"><br>
   DOB: <input type="date" name="dob" autofocus><br>
   <button type="submit" value="Submit">Submit</button>
</form>

We have set autofocus to the third input as shown below i.e. DOB, therefore the cursor is visible there itself −

DOB: <input type="date" name="dob" autofocus><br>

Updated on: 29-Jun-2020

45 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements