javascript / beginner
Snippet
Basic Event Handling
Svelte uses the 'on:' prefix followed by the event name to attach listeners to elements. You can pass a reference to a function defined in your script block.
snippet.js
1
2
3
4
5
6
7
8
9
<script>function handleClick() {alert('Button clicked!');}</script><button on:click={handleClick}>Click Me</button>
svelte
Breakdown
1
on:click={handleClick}
Assigns the 'handleClick' function to the button's click event.