Recall & Review
beginner
What does the
preventDefault event modifier do in Svelte?It stops the browser's default action for an event, like preventing a link from navigating or a form from submitting.
Click to reveal answer
beginner
How does the
stopPropagation event modifier affect event handling in Svelte?It stops the event from bubbling up to parent elements, so only the current element handles the event.
Click to reveal answer
intermediate
How do you use
preventDefault and stopPropagation together in a Svelte event?Add both modifiers to the event handler like
on:click|preventDefault|stopPropagation to stop default action and prevent bubbling.Click to reveal answer
beginner
Why might you want to use
preventDefault on a form submit event in Svelte?To stop the page from reloading so you can handle the form data with JavaScript instead.
Click to reveal answer
intermediate
What happens if you don’t use
stopPropagation on a nested clickable element in Svelte?The click event will bubble up and trigger click handlers on parent elements too, which might cause unwanted behavior.
Click to reveal answer
What does
on:click|preventDefault do in Svelte?✗ Incorrect
The
preventDefault modifier stops the browser's default action, like following a link.Which event modifier stops an event from bubbling up to parent elements?
✗ Incorrect
stopPropagation prevents the event from moving up the DOM tree.How do you write a click event in Svelte that prevents default and stops propagation?
✗ Incorrect
Modifiers are chained with pipes:
on:click|preventDefault|stopPropagation.Why use
preventDefault on a form submit event?✗ Incorrect
Preventing default stops the browser from reloading the page on submit.
If you don’t use
stopPropagation on a nested button click, what happens?✗ Incorrect
Without
stopPropagation, events bubble up and trigger parent handlers.Explain how
preventDefault and stopPropagation modifiers work in Svelte event handling.Think about what happens when you click a link or button inside nested elements.
You got /4 concepts.
Describe a real-life scenario where you would use both
preventDefault and stopPropagation in a Svelte app.Imagine a form inside a clickable card where you want to handle submit and clicks separately.
You got /4 concepts.