0
0
Svelteframework~20 mins

DOM event listeners (on:click) in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Svelte Click Event Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when the button is clicked?
Consider this Svelte component. What will be the value of count after clicking the button twice?
Svelte
<script>
  let count = 0;
  function increment() {
    count += 1;
  }
</script>
<button on:click={increment}>Click me</button>
<p>{count}</p>
AError: increment is not defined
B1
C2
D0
Attempts:
2 left
💡 Hint
Remember that each click triggers the increment function which adds 1 to count.
📝 Syntax
intermediate
2:00remaining
Which option correctly attaches a click event listener in Svelte?
Select the correct syntax to attach a click event listener that calls handleClick in Svelte.
Svelte
function handleClick() {
  alert('Clicked!');
}
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>
Attempts:
2 left
💡 Hint
Svelte uses a special directive syntax for events starting with 'on:'.
🔧 Debug
advanced
2:00remaining
Why does the click event not update the count?
Look at this Svelte component. Why does clicking the button not update the displayed count?
Svelte
<script>
  let count = 0;
  function increment() {
    count = count + 1;
  }
</script>
<button on:click="increment()">Click me</button>
<p>{count}</p>
AThe increment function does not update count correctly.
BThe button element is missing a type attribute.
CThe count variable is not reactive because it is not declared with let.
DThe on:click attribute is using quotes and a string, so the function is not called.
Attempts:
2 left
💡 Hint
Check how event handlers are attached in Svelte and how expressions are passed.
state_output
advanced
2:00remaining
What is the output after clicking the button three times?
Given this Svelte component, what will be the output shown in the paragraph after clicking the button three times?
Svelte
<script>
  let count = 0;
  function increment() {
    count += 2;
  }
</script>
<button on:click={increment}>Click me</button>
<p>{count}</p>
A6
B3
C0
DError: count is not defined
Attempts:
2 left
💡 Hint
Each click adds 2 to count. Multiply by number of clicks.
🧠 Conceptual
expert
3:00remaining
Which option correctly prevents the default action on a click event in Svelte?
You want to prevent the default action of a link when clicked using Svelte's event modifiers. Which option does this correctly?
Svelte
<script>
  function handleClick(event) {
    // prevent default here
  }
</script>
<a href="https://example.com" on:click={handleClick}>Go</a>
A<a href="https://example.com" on:click|preventDefault={handleClick}>Go</a>
B<a href="https://example.com" on:click.preventDefault={handleClick}>Go</a>
C<a href="https://example.com" on:click={event => { event.preventDefault(); handleClick(event); }}>Go</a>
D<a href="https://example.com" on:click={handleClick} preventDefault>
Attempts:
2 left
💡 Hint
Svelte uses event modifiers with a pipe symbol to modify event behavior.