CHAPTER 07
Beginner
Event Handling in Svelte
Updated: May 18, 2026
5 min read
# CHAPTER 7
Event Handling in Svelte
1. Chapter Introduction
User interaction drives every application. A button click adds to cart. A keypress searches the database. A form submit saves data. Svelte's event handling is clean, intuitive, and powerful — using theon:eventname directive to wire DOM events directly to JavaScript functions. Event modifiers like |preventDefault and |once eliminate common boilerplate.
2. Learning Objectives
-
Use
on:click,on:input,on:keydown,on:submit.
-
Apply event modifiers (
|preventDefault,|stopPropagation,|once).
-
Access the native event object with
$event.
- Create and dispatch custom events between components.
- Build a complete interactive Todo App.
3. Basic Event Handling
svelte
4. Event Modifiers
Modifiers save you from writingevent.preventDefault() etc. manually:
svelte
5. Accessing the Event Object
svelte
6. Custom Events with createEventDispatcher
Child components communicate with parents by dispatching custom events:
svelte
svelte
7. Mini Project: Interactive Todo App
svelte
8. Common Mistakes
-
Calling functions immediately:
on:click={doSomething()}callsdoSomethingat render time and assigns the return value as the handler. Useon:click={doSomething}(no parentheses).
-
Forgetting
|preventDefaulton forms: Without it, submitting a form causes a page reload, destroying all Svelte state.
9. MCQs with Answers
Question 1
What is the Svelte directive for handling click events?
Question 2
What modifier prevents the default browser behavior (like form submission)?
Question 3
What modifier ensures an event listener fires only once?
Question 4
When passing a handler with parameters, what is the correct syntax?
Question 5
What Svelte function enables custom event dispatching from child components?
Question 6
When a child dispatches dispatch('confirm', { data: 1 }), how does the parent access the data?
Question 7
What modifier prevents an event from bubbling to parent elements?
Question 8
What modifier improves scroll performance by telling the browser the handler won't call preventDefault?
Question 9
What is wrong with on:click={doSomething()}?
Question 10
How do you listen for keyboard events on an input?
10. Interview Questions
-
Q: How does Svelte's event modifier system compare to calling
event.preventDefault()manually?
-
Q: Explain the
createEventDispatcherpattern. How is it similar to@Outputin Angular?
11. Summary
Svelte's event handling is clean and declarative. Theon:eventname directive keeps event wiring close to the HTML it affects. Event modifiers eliminate common boilerplate. And createEventDispatcher provides a clean, type-safe way for child components to communicate upward. Together, these tools handle every user interaction scenario in a few expressive lines.
12. Next Chapter Recommendation
In Chapter 8: Conditional Rendering and Loops, we control what the user sees using{#if}, {:else}, and {#each} blocks to build a dynamic, data-driven product listing.