An inline event handler in Svelte is a way to attach a function directly to an HTML element's event attribute using the on:event syntax inside the component's markup.
Click to reveal answer
beginner
How do you write a click event handler inline in Svelte?
You write it like this: <button on:click={handleClick}>Click me</button>, where handleClick is a function defined in the script part of the component.
Click to reveal answer
intermediate
Can you pass arguments directly in an inline event handler in Svelte? How?
Yes, by using an inline arrow function: on:click={() => handleClick(arg)}. This way you call handleClick with arguments when the event happens.
Click to reveal answer
intermediate
What happens if you write on:click={handleClick()} instead of on:click={handleClick}?
Writing on:click={handleClick()} calls the function immediately during rendering, not on the click event. You want to pass the function itself without parentheses to run it only when clicked.
Click to reveal answer
beginner
Why are inline event handlers useful in Svelte?
They keep event logic close to the element, making code easier to read and maintain. They also leverage Svelte's reactive system for efficient updates.
Click to reveal answer
How do you attach a click event handler inline in Svelte?
A<button onclick='handleClick()'>Click</button>
B<button on:click={handleClick}>Click</button>
C<button onClick={handleClick}>Click</button>
D<button on-click={handleClick}>Click</button>
✗ Incorrect
Svelte uses the syntax on:event with lowercase event names and curly braces for functions.
What is the correct way to pass an argument to an inline event handler in Svelte?
Aon:click={handleClick(arg)}
Bon:click={handleClick.call(arg)}
Con:click={() => handleClick(arg)}
Don:click={handleClick}
✗ Incorrect
Using an arrow function lets you pass arguments without calling the function immediately.
What happens if you write on:click={handleClick()} in Svelte?
AIt causes a syntax error.
BThe function runs only when clicked.
CThe function never runs.
DThe function runs immediately during render.
✗ Incorrect
Parentheses call the function immediately instead of passing it as a handler.
Which of these is NOT a benefit of inline event handlers in Svelte?
Hint: Always prefix event with 'on:' in Svelte inline handlers [OK]
Common Mistakes:
Using React or Vue event syntax instead of Svelte's
Missing the colon ':' after 'on'
Using event name without any prefix
3. Given this Svelte code snippet, what will be the output when the button is clicked?
<script>
let count = 0;
function increment() {
count += 1;
}
</script>
<button on:click={increment}>Clicked {count} times</button>
medium
A. Runtime error occurs on click
B. Button text remains 'Clicked 0 times' even after clicks
C. Button text shows 'Clicked NaN times' after click
D. Button text updates to 'Clicked 1 times' after first click
Solution
Step 1: Understand the event handler effect
The increment function increases count by 1 on each click.
Step 2: Observe reactive update in Svelte
Svelte automatically updates the displayed {count} value when count changes.
Final Answer:
Button text updates to 'Clicked 1 times' after first click -> Option D
Quick Check:
State updates trigger UI change = Button text updates to 'Clicked 1 times' after first click [OK]
Hint: Svelte auto-updates UI when reactive variables change [OK]
Common Mistakes:
Expecting manual DOM update after state change
Confusing syntax causing runtime errors
Not realizing Svelte reactivity updates UI automatically
4. Identify the error in this Svelte code snippet:
<script>
function sayHi() {
alert('Hi!');
}
</script>
<button on:click=sayHi>Say Hi</button>
medium
A. Missing curly braces around the handler function
B. Event name should be capitalized as on:Click
C. Function sayHi must be declared inside onMount
D. Alert cannot be used inside event handlers
Solution
Step 1: Check event handler syntax
Svelte requires the handler to be inside curly braces: on:click={sayHi}.
Step 2: Identify the error in the code
The code uses on:click=sayHi without braces, which is invalid syntax.
Final Answer:
Missing curly braces around the handler function -> Option A
Quick Check:
Handler must be in braces: on:event={handler} [OK]
Hint: Always wrap handler in curly braces for inline events [OK]
Common Mistakes:
Omitting curly braces around handler
Capitalizing event names incorrectly
Thinking functions must be inside lifecycle hooks
5. You want to create a Svelte button that increments a counter only if the Shift key is held during the click. Which inline event handler code correctly implements this?
hard
A. <button on:click={event => { if(event.shiftKey) count += 1 }}>Increment</button>
B. <button on:click={incrementIfShift}>Increment</button>
<script>
let count = 0;
function incrementIfShift(event) {
if (event.shiftKey) count += 1;
}
</script>
C. <button on:click={event => { if(event.key === 'Shift') count++ }}>Increment</button>
D. <button on:click={incrementIfShift}>Increment</button>
<script>
let count = 0;
function incrementIfShift() {
if (event.shiftKey) count += 1;
}
</script>
Solution
Step 1: Understand event object usage in handlers
To check if Shift is pressed, the event object must be accessed and passed to the handler.
Step 2: Check each option for correct event parameter and condition
<button on:click={incrementIfShift}>Increment</button>
<script>
let count = 0;
function incrementIfShift(event) {
if (event.shiftKey) count += 1;
}
</script> correctly defines incrementIfShift(event) and checks event.shiftKey. <button on:click={incrementIfShift}>Increment</button>
<script>
let count = 0;
function incrementIfShift() {
if (event.shiftKey) count += 1;
}
</script> misses the event parameter. <button on:click={event => { if(event.shiftKey) count += 1 }}>Increment</button> uses inline arrow function correctly but does not declare count in snippet. <button on:click={event => { if(event.key === 'Shift') count++ }}>Increment</button> incorrectly checks event.key on click event.
Final Answer:
Uses named function incrementIfShift(event) that checks event.shiftKey -> Option B
Quick Check:
Pass event param and check event.shiftKey inside handler [OK]
Hint: Pass event param to handler to access shiftKey property [OK]
Common Mistakes:
Not passing event parameter to handler function
Checking event.key on click event instead of shiftKey
Using inline handler without declaring count variable
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.