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
<!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>