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?
<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>
Remember, preventDefault stops the browser's default action like page reload on form submit.
The preventDefault modifier stops the form from reloading the page. The handleClick function runs, setting clicked to true. So, the page stays, and the text updates.
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?
<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>
Think about what stopPropagation does to event bubbling.
The stopPropagation modifier stops the click event from bubbling up to the outer div. So, only innerClicked becomes true, and outerClicked stays false.
Choose the Svelte event binding that correctly applies both preventDefault and stopPropagation to a click event.
Modifiers are chained with pipes | before the handler function.
Option A correctly chains modifiers before the handler. Option A is valid syntax but order matters in Svelte; preventDefault should come before stopPropagation for expected behavior. Option A and D are invalid syntax.
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?
<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>
Consider how Svelte event modifiers differ from manual event methods.
In Svelte, using event.stopPropagation() inside a handler may not prevent Svelte's internal event handling order. Using the |stopPropagation modifier ensures Svelte stops the event at the right time. Here, the manual call is too late, so parentClick still runs.
Consider this Svelte code:
<a href="https://example.com" on:click|preventDefault|stopPropagation={handleClick}>Go</a>What happens when the link is clicked?
<a href="https://example.com" on:click|preventDefault|stopPropagation={handleClick}>Go</a>Think about what each modifier stops.
preventDefault stops the browser's default navigation. stopPropagation stops the event from bubbling up to parent elements. So, the link does not navigate, and no parent click handlers run.