|
| 1 | +--- |
| 2 | +title: on: |
| 3 | +--- |
| 4 | + |
| 5 | +```svelte |
| 6 | +<!--- copy: false ---> |
| 7 | +on:eventname={handler} |
| 8 | +``` |
| 9 | + |
| 10 | +```svelte |
| 11 | +<!--- copy: false ---> |
| 12 | +on:eventname|modifiers={handler} |
| 13 | +``` |
| 14 | + |
| 15 | +Use the `on:` directive to listen to DOM events. |
| 16 | + |
| 17 | +```svelte |
| 18 | +<!--- file: App.svelte ---> |
| 19 | +<script> |
| 20 | + let count = 0; |
| 21 | +
|
| 22 | + /** @param {MouseEvent} event */ |
| 23 | + function handleClick(event) { |
| 24 | + count += 1; |
| 25 | + } |
| 26 | +</script> |
| 27 | +
|
| 28 | +<button on:click={handleClick}> |
| 29 | + count: {count} |
| 30 | +</button> |
| 31 | +``` |
| 32 | + |
| 33 | +Handlers can be declared inline with no performance penalty. As with attributes, directive values may be quoted for the sake of syntax highlighters. |
| 34 | + |
| 35 | +```svelte |
| 36 | +<button on:click={() => (count += 1)}> |
| 37 | + count: {count} |
| 38 | +</button> |
| 39 | +``` |
| 40 | + |
| 41 | +Add _modifiers_ to DOM events with the `|` character. |
| 42 | + |
| 43 | +```svelte |
| 44 | +<form on:submit|preventDefault={handleSubmit}> |
| 45 | + <!-- the `submit` event's default is prevented, |
| 46 | + so the page won't reload --> |
| 47 | +</form> |
| 48 | +``` |
| 49 | + |
| 50 | +The following modifiers are available: |
| 51 | + |
| 52 | +- `preventDefault` — calls `event.preventDefault()` before running the handler |
| 53 | +- `stopPropagation` — calls `event.stopPropagation()`, preventing the event reaching the next element |
| 54 | +- `stopImmediatePropagation` - calls `event.stopImmediatePropagation()`, preventing other listeners of the same event from being fired. |
| 55 | +- `passive` — improves scrolling performance on touch/wheel events (Svelte will add it automatically where it's safe to do so) |
| 56 | +- `nonpassive` — explicitly set `passive: false` |
| 57 | +- `capture` — fires the handler during the _capture_ phase instead of the _bubbling_ phase |
| 58 | +- `once` — remove the handler after the first time it runs |
| 59 | +- `self` — only trigger handler if `event.target` is the element itself |
| 60 | +- `trusted` — only trigger handler if `event.isTrusted` is `true`. I.e. if the event is triggered by a user action. |
| 61 | + |
| 62 | +Modifiers can be chained together, e.g. `on:click|once|capture={...}`. |
| 63 | + |
| 64 | +If the `on:` directive is used without a value, the component will _forward_ the event, meaning that a consumer of the component can listen for it. |
| 65 | + |
| 66 | +```svelte |
| 67 | +<button on:click> The component itself will emit the click event </button> |
| 68 | +``` |
| 69 | + |
| 70 | +It's possible to have multiple event listeners for the same event: |
| 71 | + |
| 72 | +```svelte |
| 73 | +<!--- file: App.svelte ---> |
| 74 | +<script> |
| 75 | + let counter = 0; |
| 76 | + function increment() { |
| 77 | + counter = counter + 1; |
| 78 | + } |
| 79 | +
|
| 80 | + /** @param {MouseEvent} event */ |
| 81 | + function track(event) { |
| 82 | + trackEvent(event); |
| 83 | + } |
| 84 | +</script> |
| 85 | +
|
| 86 | +<button on:click={increment} on:click={track}>Click me!</button> |
| 87 | +``` |
| 88 | + |
| 89 | +> [!NOTE] |
| 90 | +> In Svelte 5+, use event attributes instead |
| 91 | +> ```svelte |
| 92 | +> <button onclick={() => alert('clicked')}>click me</button> |
| 93 | +> ``` |
| 94 | +
|
| 95 | +## Component events |
| 96 | +
|
| 97 | +Component events created with [`createEventDispatcher`](svelte#createEventDispatcher) create a `CustomEvent`. These events do not bubble. The detail argument corresponds to the `CustomEvent.detail` property and can contain any type of data. |
| 98 | +
|
| 99 | +```svelte |
| 100 | +<script> |
| 101 | + import { createEventDispatcher } from 'svelte'; |
| 102 | +
|
| 103 | + const dispatch = createEventDispatcher(); |
| 104 | +</script> |
| 105 | +
|
| 106 | +<button on:click={() => dispatch('notify', 'detail value')}>Fire Event</button> |
| 107 | +``` |
| 108 | +
|
| 109 | +Events dispatched from child components can be listened to in their parent. Any data provided when the event was dispatched is available on the `detail` property of the event object. |
| 110 | + |
| 111 | +```svelte |
| 112 | +<script> |
| 113 | + function callbackFunction(event) { |
| 114 | + console.log(`Notify fired! Detail: ${event.detail}`); |
| 115 | + } |
| 116 | +</script> |
| 117 | +
|
| 118 | +<Child on:notify={callbackFunction} /> |
| 119 | +``` |
| 120 | + |
| 121 | +> [!NOTE] |
| 122 | +> If you're planning on migrating to Svelte 5, use callback props instead. This will make upgrading easier as `createEventDispatcher` is deprecated |
| 123 | +> ```svelte |
| 124 | +> <script> |
| 125 | +> export let notify; |
| 126 | +> </script> |
| 127 | +> |
| 128 | +> <button on:click={() => notify('detail value')}>Fire Event</button> |
| 129 | +> ``` |
0 commit comments