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−
Example
<!DOCTYPE html> <html> <body> <h2>Result</h2> <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"> </form> </body> </html>
Output
This will produce the following output displaying the focus on the 2nd textbox, since we have set autofocus on the same input text −