The HTMLSelectElement interface represents a <select> HTML Element. These elements also share all of the properties and methods of other HTML elements via the HTMLElement interface.
The HTMLSelectElement interface represents a <select> HTML Element. These elements also share all of the properties and methods of other HTML elements via the HTMLElement interface.
This interface inherits the properties of HTMLElement, and of Element and Node.
HTMLSelectElement.autofocusA boolean value reflecting the autofocus HTML attribute, which indicates whether the control should have input focus when the page loads, unless the user overrides it, for example by typing in a different control. Only one form-associated element in a document can have this attribute specified.
HTMLSelectElement.disabledA boolean value reflecting the disabled HTML attribute, which indicates whether the control is disabled. If it is disabled, it does not accept clicks.
HTMLSelectElement.form Read only
An HTMLFormElement referencing the form that this element is associated with. If the element is not associated with of a <form> element, then it returns null.
HTMLSelectElement.labels Read only
HTMLSelectElement.lengthAn unsigned long The number of <option> elements in this select element.
HTMLSelectElement.multipleA boolean value reflecting the multiple HTML attribute, which indicates whether multiple items can be selected.
HTMLSelectElement.nameA string reflecting the name HTML attribute, containing the name of this control used by servers and DOM search functions.
HTMLSelectElement.options Read only
An HTMLOptionsCollection representing the set of <option> (HTMLOptionElement) elements contained by this element.
HTMLSelectElement.requiredA boolean value reflecting the required HTML attribute, which indicates whether the user is required to select a value before submitting the form.
HTMLSelectElement.selectedIndexA long reflecting the index of the first selected <option> element. The value -1 indicates no element is selected.
HTMLSelectElement.selectedOptions Read only
An HTMLCollection representing the set of <option> elements that are selected.
HTMLSelectElement.sizeA long reflecting the size HTML attribute, which contains the number of visible items in the control. The default is 1, unless multiple is true, in which case it is 4.
HTMLSelectElement.type Read only
A string representing the form control's type. When multiple is true, it returns "select-multiple"; otherwise, it returns "select-one".
HTMLSelectElement.validationMessage Read only
A string representing a localized message that describes the validation constraints that the control does not satisfy (if any). This attribute is the empty string if the control is not a candidate for constraint validation (willValidate is false), or it satisfies its constraints.
HTMLSelectElement.validity Read only
A ValidityState reflecting the validity state that this control is in.
HTMLSelectElement.valueA string reflecting the value of the form control. Returns the value property of the first selected option element if there is one, otherwise the empty string.
HTMLSelectElement.willValidate Read only
A boolean value that indicates whether the button is a candidate for constraint validation. It is false if any conditions bar it from constraint validation.
This interface inherits the methods of HTMLElement, and of Element and Node.
HTMLSelectElement.add()Adds an element to the collection of option elements for this select element.
HTMLSelectElement.blur() Deprecated
Removes input focus from this element. This method is now implemented on HTMLElement.
HTMLSelectElement.checkValidity()Checks whether the element has any constraints and whether it satisfies them. If the element fails its constraints, the browser fires a cancelable invalid event at the element (and returns false).
HTMLSelectElement.focus() Deprecated
Gives input focus to this element. This method is now implemented on HTMLElement.
HTMLSelectElement.item()Gets an item from the options collection for this <select> element. You can also access an item by specifying the index in square brackets or parentheses, without calling this method explicitly.
HTMLSelectElement.namedItem()Gets the item in the options collection with the specified name. The name string can match either the id or the name attribute of an option node. You can also access an item by specifying the name in square brackets or parentheses, without calling this method explicitly.
HTMLSelectElement.remove()Removes the element at the specified index from the options collection for this select element.
HTMLSelectElement.reportValidity()This method reports the problems with the constraints on the element, if any, to the user. If there are problems, it fires a cancelable invalid event at the element, and returns false; if there are no problems, it returns true.
HTMLSelectElement.setCustomValidity()Sets the custom validity message for the selection element to the specified message. Use the empty string to indicate that the element does not have a custom validity error.
Listen to these events using addEventListener() or by assigning an event listener to the oneventname property of this interface:
change eventFires when the user selects an option.
input eventFires when the value of an <input>, <select>, or <textarea> element has been changed.
js
/* assuming we have the following HTML <select id='s'> <option>First</option> <option selected>Second</option> <option>Third</option> </select> */ const select = document.getElementById("s"); // return the index of the selected option console.log(select.selectedIndex); // 1 // return the value of the selected option console.log(select.options[select.selectedIndex].value); // Second
A better way to track changes to the user's selection is to watch for the change event to occur on the <select>. This will tell you when the value changes, and you can then update anything you need to. See the example provided in the documentation for the change event for details.
| Specification |
|---|
| HTML Standard # htmlselectelement |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
HTMLSelectElement |
1 | 12 | 1You should watch for change events on<select> instead of watching <option> elements for events. See bug 1090602 and Multiprocess Firefox Web content compatibility for details. |
1 | 2 | 1 | ≤37 | 18 | 4 | 10.1 | 1 | 1.0 |
add |
1 | 12 | 1 | 5.5 | ≤12.1 | 3 | 4.4 | 18 | 4 | ≤12.1 | 1 | 1.0 |
autocomplete |
66 | 79 | 59 | No | 53≤12.1–15 | 9.1 | 66 | 66 | 59 | 47≤12.1–14 | 9.3 | 9.0 |
checkValidity |
4 | 12 | 4 | 10 | 9 | 5 | ≤37 | 18 | 4 | 10.1 | 4 | 1.0 |
disabled |
1 | 12 | 1 | 5.5 | 9 | 3 | 4.4 | 18 | 4 | 10.1 | 1 | 1.0 |
form |
1 | 12 | 1 | 5.5 | ≤12.1 | 3 | 4.4 | 18 | 4 | ≤12.1 | 1 | 1.0 |
item |
1 | 12 | 1 | 5.5 | ≤12.1 | 3 | 4.4 | 18 | 4 | ≤12.1 | 1 | 1.0 |
labels |
6 | 18 | 56 | No | ≤12.1 | 5.1 | 3 | 18 | 56 | ≤12.1 | 5 | 1.0 |
length |
1 | 12 | 1 | 5.5 | ≤12.1 | 3 | 4.4 | 18 | 4 | ≤12.1 | 1 | 1.0 |
multiple |
1 | 12 | 1 | 5.5 | ≤12.1 | 3 | 4.4 | 18 | 4 | ≤12.1 | 1 | 1.0 |
name |
1 | 12 | 1 | 5.5 | ≤12.1 | 3 | 4.4 | 18 | 4 | ≤12.1 | 1 | 1.0 |
namedItem |
1 | 12namedItem does not appear to take the name attribute into account (only the id attribute) on Internet Explorer and Edge. There is a bug report to Microsoft about this. |
1 | 6namedItem does not appear to take the name attribute into account (only the id attribute) on Internet Explorer and Edge. There is a bug report to Microsoft about this. |
≤12.1 | 3 | 4.4 | 18 | 4 | ≤12.1 | 1 | 1.0 |
options |
1 | 12 | 1 | 5.5 | ≤12.1 | 3 | 4.4 | 18 | 4 | ≤12.1 | 1 | 1.0 |
remove |
1 | 12 | 1 | 5.5 | ≤12.1 | 3 | 4.4 | 18 | 4 | ≤12.1 | 1 | 1.0 |
reportValidity |
40 | 17 | 49 | No | 27 | 10.1 | 40 | 40 | 64 | 27 | 10.3 | 4.0 |
required |
10 | 12 | 4 | 10 | ≤12.1 | 5.1 | ≤37 | 18 | 4 | ≤12.1 | 5 | 1.0 |
selectedIndex |
1 | 12 | 1 | 5.5 | ≤12.1 | 3 | 4.4 | 18 | 4 | ≤12.1 | 1 | 1.0 |
selectedOptions |
19 | 12 | 26 | No | 9 | 6 | 4.4 | 25 | 26 | 10.1 | 6 | 1.5 |
setCustomValidity |
4 | 12 | 4 | 10 | ≤12.1 | 5 | ≤37 | 18 | 4 | ≤12.1 | 4 | 1.0 |
size |
1 | 12 | 1 | 5.5 | ≤12.1 | 3 | 4.4 | 18 | 4 | ≤12.1 | 1 | 1.0 |
type |
1 | 12 | 1 | 1 | 2 | 3 | 4.4 | 18 | 4 | 10.1 | 1 | 1.0 |
validationMessage |
5 | 12 | 4 | 10 | ≤12.1 | 5 | ≤37 | 18 | 4 | ≤12.1 | 4 | 1.0 |
validity |
4 | 12 | 4 | 10 | ≤12.1 | 5 | ≤37 | 18 | 4 | ≤12.1 | 4 | 1.0 |
value |
1 | 12 | 1 | 5.5 | ≤12.1 | 3 | 4.4 | 18 | 4 | ≤12.1 | 1 | 1.0 |
willValidate |
2 | 12 | 4 | 10 | ≤12.1 | 4 | ≤37 | 18 | 4 | ≤12.1 | 3 | 1.0 |
<select> HTML element, which implements this interface.
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement