By the end of this lesson, you will be able to:
on:<event>
syntax:information_source: What are Svelte Events? Events in Svelte are actions that happen when users interact with your website. Just like JavaScript, these events help make your website interactive!
Here are some events you'll use often:
:memo: Note Svelte events work with the Document Object Model (DOM). The DOM is like a map of all the elements on your webpage!
You can create event handlers directly in your template using the on:<event>
syntax.
Let's start with the most common event - the click event. We handle it using on:click
.
App.svelte
<script>
function clickButton() \{
alert('You clicked on the button');
\}
</script>
<button on:click=\{clickButton\}>
Click me
</button>
on:click
event handlerclickButton
function:bulb: Tip Always give your functions clear names that describe what they do.
clickButton
tells us exactly when this function will run!
Event modifiers help you control how events behave. Think of them as special instructions you can add to your events!
Svelte has eight event modifiers, but let's focus on the two most important ones:
Event Modifier | What it Does | When to Use It |
---|---|---|
once |
Runs the event handler only one time | Perfect for preventing accidental double-clicks on buttons |
preventDefault |
Stops the default browser behavior | Use when you want to handle form submissions or link clicks yourself |
To add a modifier to an event, use the |
symbol (vertical bar):
<button on:click|once=\{clickButton\}>Click me</button>
once
ModifierLet's see how the once
modifier prevents multiple clicks:
App.svelte
<script>
function clickButton() \{
alert('You clicked on the button');
\}
</script>
<button on:click|once=\{clickButton\}>
Click me
</button>
First click:
Second click onwards:
No more alert coming out
preventDefault
ModifierThe preventDefault
modifier stops the browser's default behavior. Here's how it works with links:
App.svelte
<script>
function clickLink() \{
alert("Hello World");
\}
</script>
<a href="https://telebort.com" on:click|preventDefault=\{clickLink\}>
Telebort
</a>
When you click the link:
Sometimes you want to create custom components that can handle events. Event forwarding lets your custom components pass events up to their parent components.
Let's see this in action:
App.svelte
<script>
import CustomButton from './CustomButton.svelte';
function clickButton() \{
alert('You clicked on the button');
\}
</script>
<CustomButton on:click=\{clickButton\} />
CustomButton.svelte
<button on:click>
Click me
</button>
<style>
button \{
background: orange;
color: white;
border-radius: 5px;
padding: 15px;
\}
</style>
:memo: Note Notice how
on:click
in CustomButton.svelte doesn't have a function? That's event forwarding! The event gets passed up to the parent component.
Let's review what you've learned about Svelte events:
:white_check_mark: Events make your website interactive by responding to user actions
:white_check_mark: Event handlers use the on:<event>
syntax (like on:click
)
:white_check_mark: Event modifiers control how events behave:
once
- runs only one timepreventDefault
- stops default browser behavior
:white_check_mark: Event forwarding lets custom components pass events to parent componentsTry these exercises to master Svelte events:
preventDefault
to stop the page from refreshing:bulb: Tip Remember: The best way to learn events is by practicing! Start with simple click events and gradually try more complex interactions.