An event in Svelte is a signal that something happened, like a user clicking a button or typing in a field. It lets the app know to respond.
Click to reveal answer
beginner
Why do events drive user interaction?
Events let the app listen and react to what the user does, making the app feel alive and responsive.
Click to reveal answer
beginner
How do you listen to a click event on a button in Svelte?
Use on:click on the button element, like <button on:click={handleClick}>Click</button>.
Click to reveal answer
beginner
What happens inside the event handler function?
The event handler runs code to update the app or UI based on what the user did, like showing a message or changing a value.
Click to reveal answer
beginner
How do events improve user experience?
They make apps interactive and dynamic, so users get immediate feedback and can control what happens next.
Click to reveal answer
In Svelte, how do you attach a click event to a button?
A<button event=click>
B<button click={handler}>
C<button @click={handler}>
D<button on:click={handler}>
✗ Incorrect
Svelte uses the syntax on:eventName to listen to events, so on:click is correct.
What does an event handler do?
ADeletes user data
BRuns code when an event happens
CCreates a new event
DStops the app from running
✗ Incorrect
An event handler runs code in response to an event, like a click or input.
Why are events important for user interaction?
AThey hide UI elements
BThey slow down the app
CThey let the app respond to user actions
DThey prevent user input
✗ Incorrect
Events allow the app to react to what the user does, making it interactive.
Which of these is NOT an example of an event?
Asave
Bhover
Cinput
Dclick
✗ Incorrect
'save' is not a standard event; click, hover, and input are common events.
How does Svelte improve event handling compared to plain JavaScript?
ASimpler syntax with on:event
BRequires more code
CNo event handling possible
DEvents are handled automatically without code
✗ Incorrect
Svelte uses a simple on:event syntax that is easy to read and write.
Explain how events help make a Svelte app interactive.
Think about what happens when you click a button.
You got /4 concepts.
Describe the syntax to listen to a user event in Svelte and what happens when the event occurs.
Look at how you add on:click to a button.
You got /4 concepts.
Practice
(1/5)
1. In Svelte, what does the on:click directive do?
easy
A. It listens for a click event and runs the specified code when clicked.
B. It styles the element to look clickable.
C. It disables the element from being clicked.
D. It automatically submits a form when clicked.
Solution
Step 1: Understand the purpose of on:click
The on:click directive in Svelte listens for when the user clicks on an element.
Step 2: What happens when clicked
When the element is clicked, the code inside the event handler runs, allowing the app to respond.
Final Answer:
It listens for a click event and runs the specified code when clicked. -> Option A
Quick Check:
Event listener = on:click runs code on click [OK]
Hint: Remember: on:eventName runs code when event happens [OK]
Common Mistakes:
Thinking on:click changes styles automatically
Assuming on:click disables the element
Believing on:click submits forms by default
2. Which of the following is the correct syntax to handle a submit event on a form in Svelte?
easy
A.
B.
C.
D.
Solution
Step 1: Recall Svelte event syntax
Svelte uses on:eventName to listen to events, so on:submit is correct.
Step 2: Check other options
Options A, B, and D use incorrect syntax not supported by Svelte.
Final Answer:
<form on:submit={handleSubmit}> -> Option C
Quick Check:
Svelte event syntax = on:eventName [OK]
Hint: Use on:eventName={handler} for events in Svelte [OK]
Common Mistakes:
Using camelCase like onSubmit instead of on:submit
Using @submit which is Vue syntax
Placing 'on' after event name
3. What will be the output when the button below is clicked?
<script>
let count = 0;
function increment() {
count += 1;
}
</script>
<button on:click={increment}>Clicked {count} times</button>
medium
A. The button text updates to show how many times it was clicked.
B. The button text never changes.
C. The page reloads on each click.
D. An error occurs because count is not reactive.
Solution
Step 1: Understand reactivity of count
In Svelte, variables declared with let are reactive, so updating count updates the UI.
Step 2: What happens on click
Clicking the button calls increment(), which adds 1 to count, updating the displayed text.
Final Answer:
The button text updates to show how many times it was clicked. -> Option A
Quick Check:
Reactive variable update = UI changes [OK]
Hint: Svelte updates UI automatically when reactive variables change [OK]
Common Mistakes:
Thinking count is not reactive without $ prefix
Expecting page reload on click
Assuming button text stays static
4. Identify the error in this Svelte code snippet:
<script>
let name = '';
function updateName(event) {
name = event.target.value;
}
</script>
<input type="text" on:input={updateName} />
<p>Hello, {name}!</p>
medium
A. The event handler should be on:change, not on:input.
B. There is no error; the code updates name as the user types.
C. The input element is missing a bind:value directive.
D. The function updateName should use event.currentTarget.value instead of event.target.value.
Solution
Step 1: Check event handler usage
The on:input event fires whenever the user types, which is appropriate here.
Step 2: Verify event property usage
Using event.target.value correctly accesses the input's current value.
Final Answer:
There is no error; the code updates name as the user types. -> Option B
Quick Check:
on:input with event.target.value updates state correctly [OK]
Hint: on:input updates live; event.target.value is correct [OK]
Common Mistakes:
Confusing on:input with on:change
Thinking event.currentTarget is required
Believing bind:value is mandatory for updates
5. You want to create a Svelte component that counts clicks but only updates the count if the Shift key is held during the click. Which event handler code correctly implements this?
hard
A. { if(event.key === 'Shift') count++ }}>Click me
B. Click me
C. { if(count.shiftKey) count += 1 }}>Click me
D. { if(event.shiftKey) count += 1 }}>Click me
Solution
Step 1: Check event object usage
The event object has a shiftKey boolean indicating if Shift was pressed during the click.
Step 2: Verify correct syntax and logic
{ if(event.shiftKey) count += 1 }}>Click me uses an arrow function with a condition checking event.shiftKey before incrementing count, which is correct.
Step 3: Identify errors in other options
Click me misuses expression syntax; { if(count.shiftKey) count += 1 }}>Click me incorrectly accesses count.shiftKey; { if(event.key === 'Shift') count++ }}>Click me checks event.key which is undefined on click events.