Skip to content

Commit

Permalink
Fix issue 28418: Add a page for HTMLSelectElement.value (#33805)
Browse files Browse the repository at this point in the history
* Fix issue 28418: Add a page for HTMLSelectElement.value

* Note that value is settable
  • Loading branch information
wbamberg authored May 30, 2024
1 parent 6660c82 commit 7f38ec3
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions files/en-us/web/api/htmlselectelement/value/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
title: "HTMLSelectElement: value property"
short-title: value
slug: Web/API/HTMLSelectElement/value
page-type: web-api-instance-property
browser-compat: api.HTMLSelectElement.value
---

{{ APIRef("HTML DOM") }}

The **`HTMLSelectElement.value`** property contains the value of the first selected {{htmlelement("option")}} element associated with this {{htmlelement("select")}} element.

This property can also be set directly, for example to set a default value based on some condition.

## Value

A string containing the value of the first selected {{htmlelement("option")}} element in this {{htmlelement("select")}} element, or the empty string if no options are selected.

## Examples

### Retrieving the selected value

```html
<label for="bird-select">Choose a bird:</label>

<select name="birds" id="bird-select">
<option value="">--Please choose an option--</option>
<option value="Scarlet ibis">Scarlet ibis</option>
<option value="Marabou stork">Marabou stork</option>
<option value="Roseate spoonbill">Roseate spoonbill</option>
</select>

<pre id="log"></pre>
```

```js
const logElement = document.querySelector("#log");
function log(text) {
logElement.innerText = text;
}

const select = document.querySelector("#bird-select");
select.addEventListener("change", () => {
log(`Selection: ${select.value}`);
});
```

```css hidden
#log {
height: 20px;
padding: 0.5rem;
border: 1px solid black;
}
```

{{EmbedLiveSample("Retrieving the selected value")}}

## Specifications

{{Specifications}}

## Browser compatibility

{{Compat}}

## See also

- The {{HTMLElement("select")}} HTML element, implementing this interface.

0 comments on commit 7f38ec3

Please sign in to comment.