mirror of https://github.com/sveltejs/svelte
14 lines
650 B
14 lines
650 B
6 years ago
|
---
|
||
|
title: Select bindings
|
||
|
---
|
||
|
|
||
4 years ago
|
We can also use `bind:value` with `<select>` elements. Update line 20:
|
||
6 years ago
|
|
||
2 years ago
|
```svelte
|
||
6 years ago
|
<select bind:value={selected} on:change="{() => answer = ''}">
|
||
|
```
|
||
|
|
||
|
Note that the `<option>` values are objects rather than strings. Svelte doesn't mind.
|
||
|
|
||
3 years ago
|
> Because we haven't set an initial value of `selected`, the binding will set it to the default value (the first in the list) automatically. Be careful though — until the binding is initialised, `selected` remains undefined, so we can't blindly reference e.g. `selected.id` in the template. If your use case allows it, you could also set an initial value to bypass this problem.
|