What is HTML autofocus Attribute?

HTML, HTML Attributes , 0 Comments

autofocus is a boolean attribute and it makes sure that an element will get focus automatically as soon as page loads, unless the user overrides it.

This attribute can only be defined by one form-associated component in a document. If there are multiple elements with autofocus attribute, the initial focus will be given to the first element with the set of attributes inserted, usually the first such element on the page.

Tag Support

You can use this attribute on <button>, <input>, <select>, <textarea>.

Setting this property doesn’t set the focus to the associated element: it merely tells the browser to focus on it when the element is inserted in the document. Setting it after the insertion, which is most of the time after the document load, has no visible effect.

Example

<button type="button" autofocus>Click Me!</button>

<input type="text" name="name" autofocus>

<textarea autofocus>
	the textarea autofocus example.
</textarea>

<select autofocus>
  <option value="value1">Value 1</option>
  <option value="value2">Value 2</option>
</select>

Browser Support

Data on support for the autofocus feature across the major browsers from caniuse.com

Spread the love

Leave a Reply

avatar
  Subscribe  
Notify of