0
0
Svelteframework~20 mins

Event modifiers (preventDefault, stopPropagation) in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Event Modifier Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when clicking the button with preventDefault modifier?

Consider this Svelte component:

<script>
  let clicked = false;
  function handleClick(event) {
    clicked = true;
  }
</script>

<form on:submit|preventDefault={handleClick}>
  <button type="submit">Submit</button>
</form>

<p>Clicked: {clicked}</p>

What will be the value of clicked after clicking the button?

Svelte
<script>
  let clicked = false;
  function handleClick(event) {
    clicked = true;
  }
</script>

<form on:submit|preventDefault={handleClick}>
  <button type="submit">Submit</button>
</form>

<p>Clicked: {clicked}</p>
Aclicked becomes true and the form does NOT reload the page.
Bclicked remains false and the form reloads the page.
Cclicked becomes true and the form reloads the page.
Dclicked remains false and the form does NOT reload the page.
Attempts:
2 left
💡 Hint

Remember, preventDefault stops the browser's default action like page reload on form submit.

component_behavior
intermediate
2:00remaining
How does stopPropagation affect nested click events?

Given this Svelte component:

<script>
  let outerClicked = false;
  let innerClicked = false;
  function outer() { outerClicked = true; }
  function inner() { innerClicked = true; }
</script>

<div on:click={outer} style="padding: 20px; background: lightblue;">
  Outer Div
  <button on:click|stopPropagation={inner}>Click Me</button>
</div>

<p>Outer clicked: {outerClicked}</p>
<p>Inner clicked: {innerClicked}</p>

What happens when the button is clicked?

Svelte
<script>
  let outerClicked = false;
  let innerClicked = false;
  function outer() { outerClicked = true; }
  function inner() { innerClicked = true; }
</script>

<div on:click={outer} style="padding: 20px; background: lightblue;">
  Outer Div
  <button on:click|stopPropagation={inner}>Click Me</button>
</div>

<p>Outer clicked: {outerClicked}</p>
<p>Inner clicked: {innerClicked}</p>
ANeither innerClicked nor outerClicked become true.
BBoth innerClicked and outerClicked become true.
COnly outerClicked becomes true; innerClicked remains false.
DOnly innerClicked becomes true; outerClicked remains false.
Attempts:
2 left
💡 Hint

Think about what stopPropagation does to event bubbling.

📝 Syntax
advanced
2:00remaining
Which option correctly uses both preventDefault and stopPropagation modifiers?

Choose the Svelte event binding that correctly applies both preventDefault and stopPropagation to a click event.

A<button on:click|preventDefault|stopPropagation={handleClick}>Click</button>
B<button on:click|stopPropagation|preventDefault={handleClick}>Click</button>
C<button on:click={handleClick|preventDefault|stopPropagation}>Click</button>
D<button on:click|preventDefault={handleClick}|stopPropagation>Click</button>
Attempts:
2 left
💡 Hint

Modifiers are chained with pipes | before the handler function.

🔧 Debug
advanced
2:00remaining
Why does stopPropagation not work as expected in this code?

Look at this Svelte snippet:

<script>
  let count = 0;
  function parentClick() { count += 1; }
  function childClick(event) {
    event.stopPropagation();
    count += 10;
  }
</script>

<div on:click={parentClick}>
  <button on:click={childClick}>Add</button>
</div>

<p>Count: {count}</p>

Clicking the button increases count by 11. Why?

Svelte
<script>
  let count = 0;
  function parentClick() { count += 1; }
  function childClick(event) {
    event.stopPropagation();
    count += 10;
  }
</script>

<div on:click={parentClick}>
  <button on:click={childClick}>Add</button>
</div>

<p>Count: {count}</p>
AThe event object is undefined, so stopPropagation does nothing.
BThe parentClick function is called manually inside childClick.
CstopPropagation is called too late; Svelte modifiers are needed to stop bubbling.
DThe button's default action triggers parentClick despite stopPropagation.
Attempts:
2 left
💡 Hint

Consider how Svelte event modifiers differ from manual event methods.

🧠 Conceptual
expert
2:00remaining
What is the combined effect of preventDefault and stopPropagation modifiers on a link click?

Consider this Svelte code:

<a href="https://example.com" on:click|preventDefault|stopPropagation={handleClick}>Go</a>

What happens when the link is clicked?

Svelte
<a href="https://example.com" on:click|preventDefault|stopPropagation={handleClick}>Go</a>
AThe browser navigates to the link, but parent click handlers do NOT run.
BThe browser does NOT navigate to the link, and no parent click handlers run.
CThe browser does NOT navigate to the link, but parent click handlers still run.
DThe browser navigates to the link, and parent click handlers run.
Attempts:
2 left
💡 Hint

Think about what each modifier stops.