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
Copy file name to clipboardExpand all lines: documentation/docs/03-template-syntax/11-bind.md
+52-2Lines changed: 52 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -53,6 +53,19 @@ 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
+
You can give the input a default value by setting the `defaultValue` property. This way, when the input is part of a form and its `form.reset()` method is invoked, it will revert to that value instead of the empty string. Note that for the initial render the value of the binding takes precedence if it's not `null` or `undefined`.
57
+
58
+
```svelte
59
+
<script>
60
+
let value = $state('');
61
+
</script>
62
+
63
+
<form>
64
+
<input bind:value defaultValue="x">
65
+
<input type="reset" value="Reset">
66
+
</form>
67
+
```
68
+
56
69
## `<input bind:checked>`
57
70
58
71
Checkbox and radio inputs can be bound with `bind:checked`:
@@ -64,16 +77,29 @@ Checkbox and radio inputs can be bound with `bind:checked`:
64
77
</label>
65
78
```
66
79
80
+
You can give the input a default value by setting the `defaultChecked` property. This way, when the input is part of a form and its `form.reset()` method is invoked, it will revert to that value instead of `false`. Note that for the initial render the value of the binding takes precedence if it's not `null` or `undefined`.
You can give the group a default value by setting the `checked` property on the elements that should be selected initially. This way, when the input is part of a form and its `form.reset()` method is invoked, it will revert to that value instead of the empty string (for radio groups) or the empty array (for checkbox groups). Note that for the initial render the value of the binding takes precedence if it's not `null` or `undefined`.
> [!NOTE]`bind:group` only works if the inputs are in the same Svelte component.
92
132
93
133
## `<input bind:files>`
@@ -146,6 +186,16 @@ When the value of an `<option>` matches its text content, the attribute can be o
146
186
</select>
147
187
```
148
188
189
+
You can give the select a default value by setting the `selected` property on the elements that should be selected initially. This way, when the select is part of a form and its `form.reset()` method is invoked, it will revert to that value instead of the empty string (for single-value selects) or the empty array (for mult-value selects). Note that for the initial render the value of the binding takes precedence if it's not `null` or `undefined`.
190
+
191
+
```svelte
192
+
<select bind:value={selected}>
193
+
<option value={a}>a</option>
194
+
<option value={b} selected>b</option>
195
+
<option value={c}>c</option>
196
+
</select>
197
+
```
198
+
149
199
## `<audio>`
150
200
151
201
`<audio>` elements have their own set of bindings — five two-way ones...
0 commit comments