HTML autofocus Attribute

HTMLWeb DevelopmentFront End Technology

The autofocus attribute in HTML is used for multiple elements and sets the focus on that particular element when the page loads. The element that constitutes the autofocus attributes are.

<button>, <input>, <select> and <textarea>.

Let us now see an example to implement the autofocus attribute in HTML−


 Live Demo

<!DOCTYPE html>
<form action="" accept-charset="ISO-8859-1">
   <label for="stinfo">Student:</label>
   <input type="text" name ="stinfo"><br>
   <label for="sub">Subject:</label>
   <input type="text" name ="sub" autofocus><br>
   <label for="rank">Rank:</label>
   <input type="number" name ="rank"><br>
   <label for="marks">Marks:</label>
   <input type="number" name ="marks"><br><br>
   <input type="submit">


This will produce the following output displaying the focus on the 2nd textbox, since we have set autofocus on the same input text − 

Published on 19-Jun-2019 14:37:09