Skip to content

Commit e5c840c

Browse files
authored
docs: adjust bind:files section (#13230)
closes #13156
1 parent 80e30f7 commit e5c840c

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

documentation/docs/02-template-syntax/08-bindings.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,25 +42,30 @@ Numeric input values are coerced; even though `input.value` is a string as far a
4242
<input type="range" bind:value={num} />
4343
```
4444

45-
On `<input>` elements with `type="file"`, you can use `bind:files` to get the [`FileList` of selected files](https://developer.mozilla.org/en-US/docs/Web/API/FileList). It is readonly.
45+
On `<input>` elements with `type="file"`, you can use `bind:files` to get the [`FileList` of selected files](https://developer.mozilla.org/en-US/docs/Web/API/FileList). When you want to update the files programmatically, you always need to use a `FileList` object.
4646

4747
```svelte
48+
<script>
49+
let files = $state();
50+
51+
function clear() {
52+
files = new FileList(); // null or undefined doesn't work
53+
}
54+
</script>
55+
4856
<label for="avatar">Upload a picture:</label>
4957
<input accept="image/png, image/jpeg" bind:files id="avatar" name="avatar" type="file" />
58+
<button onclick={clear}>clear</button>
5059
```
5160

52-
If you're using `bind:` directives together with `on:` directives, the order that they're defined in affects the value of the bound variable when the event handler is called.
61+
If you're using `bind:` directives together with `on` event attributes, the binding will always fire before the event attribute.
5362

5463
```svelte
5564
<script>
5665
let value = 'Hello World';
5766
</script>
5867
59-
<input
60-
on:input={() => console.log('Old value:', value)}
61-
bind:value
62-
on:input={() => console.log('New value:', value)}
63-
/>
68+
<input oninput={() => console.log('New value:', value)} bind:value />
6469
```
6570

6671
Here we were binding to the value of a text input, which uses the `input` event. Bindings on other elements may use different events such as `change`.

0 commit comments

Comments
 (0)