The autofocus attribute of the <button> element is used to set focus to the button whenever page loads.
Following is the syntax −
<button type="button" autofocus>
Above, we have set autofocus to a button. Let us now see an example to implement the autofocus attribute of the <button> element −
Example
<!DOCTYPE html> <html> <body> <h2>Demo Heading</h2> <p>This is a demo line.</p> <button type="button" autofocus >Demo</button> </body> </html>
Output
This will produce the following output displaying the focus is on button 1 −
In the above example, we have set the autofocus attribute for the button element −
<button type="button" autofocus > Demo </button>
Now, when the page loads, the focus would be on the button itself and the same would appear to the visitor on the web page.