docs.rodeo

MDN Web Docs mirror

HTMLSelectElement

{{APIRef("HTML DOM")}} 

The HTMLSelectElement interface represents a {{HTMLElement("select")}}  HTML Element. These elements also share all of the properties and methods of other HTML elements via the {{domxref("HTMLElement")}}  interface.

{{InheritanceDiagram}} 

Instance properties

This interface inherits the properties of {{domxref("HTMLElement")}} , and of {{domxref("Element")}}  and {{domxref("Node")}} .

Instance methods

This interface inherits the methods of {{domxref("HTMLElement")}} , and of {{domxref("Element")}}  and {{domxref("Node")}} .

Events

This interface inherits the events of {{domxref("HTMLElement")}} , and of {{domxref("Element")}}  and {{domxref("Node")}} .

Listen to these events using {{domxref("EventTarget/addEventListener", "addEventListener()")}}  or by assigning an event listener to the oneventname property of this interface:

Example

Get information about the selected option

/* 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 {{domxref("HTMLElement/change_event", "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.

Specifications

{{Specifications}} 

Browser compatibility

{{Compat}} 

See also

In this article

View on MDN