We use DOM event listeners to make web pages interactive. The on:click lets us run code when someone clicks a button or element.
DOM event listeners (on:click) in Svelte
<button on:click={handleClick}>Click me</button>
<script>
function handleClick() {
// code to run on click
}
</script>The on:click attribute listens for click events on the element.
The function inside { } runs when the click happens.
<button on:click={() => alert('Hello!')}>Say Hello</button><button on:click={increment}>Add 1</button>
<script>
let count = 0;
function increment() {
count += 1;
}
</script>on:click to null, clicking does nothing.<button on:click={null}>No action</button>This Svelte component shows a message and a button. When you click the button, the message changes. The button has an aria-label for accessibility.
<script> let message = 'Click the button'; function handleClick() { message = 'Button clicked!'; } </script> <main> <p>{message}</p> <button on:click={handleClick} aria-label="Click to change message">Click me</button> </main>
Click event listeners run very fast and respond immediately to user clicks.
Remember to use descriptive function names like handleClick to keep code clear.
Use aria-label on clickable elements for better accessibility.
on:click lets you run code when an element is clicked.
Use it to make buttons and other elements interactive.
Always keep accessibility in mind by adding labels and clear text.
Practice
on:click directive do in a Svelte component?Solution
Step 1: Understand the purpose of
Theon:clickon:clickdirective listens for click events on an element.Step 2: Identify what happens on click
When the element is clicked, the code inside the handler runs.Final Answer:
It runs code when the user clicks on the element. -> Option BQuick Check:
on:clickruns code on click [OK]
- Thinking it styles the element automatically
- Believing it disables clicks
- Assuming it changes text without code
handleClick when a button is clicked in Svelte?Solution
Step 1: Recall Svelte event syntax
Svelte useson:eventName={handler}syntax for event listeners.Step 2: Match correct syntax
on:click={handleClick}is the correct way to attach the click handler.Final Answer:
<button on:click={handleClick}>Click me</button> -> Option AQuick Check:
Svelte uses on:eventName syntax [OK]
- Using React style onClick instead of on:click
- Swapping event and on keywords
- Using @click which is Vue syntax
<script>
let count = 0;
function increment() {
count += 1;
}
</script>
<button on:click={increment}>Clicked {count} times</button>What will the button display after clicking it 3 times?
Solution
Step 1: Understand the initial state and function
The variablecountstarts at 0. Theincrementfunction adds 1 tocounteach click.Step 2: Calculate count after 3 clicks
After 3 clicks,countbecomes 0 + 3 = 3.Final Answer:
Clicked 3 times -> Option CQuick Check:
count increments by 1 per click [OK]
- Forgetting to update count on click
- Assuming count resets
- Confusing initial value with updated value
<button on:click=increment()>Click me</button>
<script>
let count = 0;
function increment() {
count += 1;
}
</script>Solution
Step 1: Check event handler syntax
In Svelte, event handlers must be assigned a function reference inside curly braces, not called immediately.Step 2: Identify the error
Usingon:click=increment()calls the function immediately instead of on click. Correct ison:click={increment}.Final Answer:
The function call should be inside curly braces: on:click={increment} -> Option AQuick Check:
Use on:click={function} not on:click=function() [OK]
- Calling function immediately instead of passing reference
- Using React style onClick instead of on:click
- Placing variables inside HTML tags incorrectly
Solution
Step 1: Check event handler syntax correctness
<script> let count = 0; function increment() { count += 1; } function reset() { count = 0; } </script> <button on:click={increment}>Increment</button> <button on:click={reset}>Reset</button> <p>Count: {count}</p>useson:click={increment}andon:click={reset}, which is correct Svelte syntax.Step 2: Identify errors in other options
<script> let count = 0; function increment() { count += 1; } function reset() { count = 0; } </script> <button on:click={increment()}>Increment</button> <button on:click={reset()}>Reset</button> <p>Count: {count}</p>calls functions immediately, which is wrong.<script> let count = 0; function increment() { count += 1; } function reset() { count = 0; } </script> <button on:click=increment>Increment</button> <button on:click=reset>Reset</button> <p>Count: {count}</p>misses curly braces.<script> let count = 0; function increment() { count += 1; } function reset() { count = 0; } </script> <button onClick={increment}>Increment</button> <button onClick={reset}>Reset</button> <p>Count: {count}</p>uses React styleonClick, invalid in Svelte.Final Answer:
Correct syntax: on:click={increment} and on:click={reset} -> Option DQuick Check:
Use on:click={function} with curly braces [OK]
- Calling functions immediately in event handlers
- Omitting curly braces around handlers
- Using React event syntax instead of Svelte
Ready to prove it?
Pass the test and this pattern moves unknown
