HTML autofocus Attribute (original) (raw)
Last Updated : 24 Feb, 2025
The HTML **autofocus attribute is a powerful tool that enhances user experience by automatically setting the focus to a specific element when a webpage loads.
**Syntax
******
**Note: This attribute is boolean, meaning it can either be present or absent.
**Supported Tags
Element | Purpose |
---|---|
Button | Automatically focuses the button when the page loads. |
Input | Automatically focuses the input field when the page loads. |
Select | Automatically focuses the select dropdown when the page loads. |
Textarea | Automatically focuses the text area when the page loads. |
**Tip: It is important to note that only one element per document can effectively have the autofocus attribute. If multiple elements include autofocus, the first one in the DOM (Document Object Model) will receive focus
HTML autofocus Attribute Examples
**Example 1: Focusing on the First Name "Input text Field" on Page Load
HTML `
HTML autofocus AttributeHTML autofocus Attribute
First Name:`
**Output:
Example 2: Autofocus on a Textarea Field
HTML `
HTML autofocus AttributeHTML autofocus Attribute
`
**Output:
HTML autofocus Attribute example output