You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* tests
* typings
* implement for defaultValue/defaultChecked on inputs
* docs (draft)
* selected
* fix test
* remove
* tweak
* changeset
* untrack reads, they could be inside an effect
* Apply suggestions from code review
Co-authored-by: Rich Harris <[email protected]>
* handle select reset case
* handle reset case specifically: use different props/queries in that case
* enhance test
* fix
---------
Co-authored-by: Rich Harris <[email protected]>
Copy file name to clipboardexpand all lines: documentation/docs/03-template-syntax/11-bind.md
+41-2
Original file line number
Diff line number
Diff line change
@@ -53,6 +53,22 @@ In the case of a numeric input (`type="number"` or `type="range"`), the value wi
53
53
54
54
If the input is empty or invalid (in the case of `type="number"`), the value is `undefined`.
55
55
56
+
If an `<input>` has a `defaultValue` and is part of a form, it will revert to that value instead of the empty string when the form is reset. Note that for the initial render the value of the binding takes precedence unless it is `null` or `undefined`.
57
+
58
+
```svelte
59
+
<script>
60
+
let value = $state('');
61
+
</script>
62
+
63
+
<form>
64
+
<input bind:value defaultValue="not the empty string">
65
+
<input type="reset" value="Reset">
66
+
</form>
67
+
```
68
+
69
+
> [!NOTE]
70
+
> Use reset buttons sparingly, and ensure that users won't accidentally click them while trying to submit the form.
71
+
56
72
## `<input bind:checked>`
57
73
58
74
Checkbox and radio inputs can be bound with `bind:checked`:
@@ -64,16 +80,29 @@ Checkbox and radio inputs can be bound with `bind:checked`:
64
80
</label>
65
81
```
66
82
83
+
If an `<input>` has a `defaultChecked` attribute and is part of a form, it will revert to that value instead of `false` when the form is reset. Note that for the initial render the value of the binding takes precedence unless it is `null` or `undefined`.
<!-- grouped radio inputs are mutually exclusive -->
@@ -146,6 +175,16 @@ When the value of an `<option>` matches its text content, the attribute can be o
146
175
</select>
147
176
```
148
177
178
+
You can give the `<select>` a default value by adding a `selected` attribute to the`<option>` (or options, in the case of `<select multiple>`) that should be initially selected. If the `<select>` is part of a form, it will revert to that selection when the form is reset. Note that for the initial render the value of the binding takes precedence if it's not `undefined`.
179
+
180
+
```svelte
181
+
<select bind:value={selected}>
182
+
<option value={a}>a</option>
183
+
<option value={b} selected>b</option>
184
+
<option value={c}>c</option>
185
+
</select>
186
+
```
187
+
149
188
## `<audio>`
150
189
151
190
`<audio>` elements have their own set of bindings — five two-way ones...
0 commit comments