W3cubDocs

/DOM

HTMLSelectElement.autofocus

The HTMLSelectElement.autofocus property is a Boolean that reflects the autofocus HTML attribute, which indicates whether the associated <select> element will get input focus when the page loads, unless the user overrides it.

Only one form-associated element in a document can have this attribute specified. If there are several, the first element with the attribute set inserted, usually the first such element on the page, get the initial focus.

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

Syntax

aBool = aSelectElement.autofocus; // Get the value of autofocus
aSelectElement.autofocus = aBool; // Set the value of autofocus

Example

HTML

<select id="mySelect" autofocus>
  <option>Option 1</option>
  <option>Option 2</option>
</select>

JavaScript

// Check if the autofocus attribute on the <select>
var hasAutofocus = document.getElementById('mySelect').autofocus;

Specifications

Browser compatibilityUpdate compatibility data on GitHub

Desktop
Chrome Edge Firefox Internet Explorer Opera Safari
Basic support Yes Yes Yes 10 Yes Yes
Mobile
Android webview Chrome for Android Edge Mobile Firefox for Android Opera for Android iOS Safari Samsung Internet
Basic support Yes Yes Yes 4 No No ?

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/autofocus