A. Only 'Button clicked' alert shows; 'Body clicked' is blocked.
B. Both alerts 'Button clicked' and 'Body clicked' show.
C. Only 'Body clicked' alert shows; button click is ignored.
D. No alerts show because event is stopped.
Solution
Step 1: Understand stopPropagation effect
The stopPropagation modifier stops the event from bubbling up to parent elements like document.body.
Step 2: Analyze event listeners
The button's click handler runs and shows 'Button clicked'. The body's click listener does not run because propagation is stopped.
Final Answer:
Only 'Button clicked' alert shows; 'Body clicked' is blocked. -> Option A
Quick Check:
stopPropagation blocks event bubbling [OK]
Hint: stopPropagation stops event bubbling to parents [OK]
Common Mistakes:
Assuming both alerts show
Confusing stopPropagation with preventDefault
Thinking event is completely canceled
4. You wrote this Svelte code:
<a href='https://example.com' on:click|preventDefault|stopPropagation={goSomewhere}>Link</a> function goSomewhere() { console.log('Navigating'); }
But clicking the link still navigates to the URL. What is the likely problem?
medium
A. The event modifiers are correct; the problem is elsewhere.
B. The function goSomewhere does not call event.preventDefault().
C. You forgot to pass the event parameter to goSomewhere.
D. The modifiers are in wrong order; preventDefault must come after stopPropagation.
Solution
Step 1: Check event modifiers usage
In Svelte, preventDefault and stopPropagation modifiers automatically call the respective event methods before the handler runs.
Step 2: Verify code correctness
The modifiers are correctly applied and order does not matter. The function does not need to call event.preventDefault()</> manually.
Step 3: Consider other causes
If navigation still happens, the problem is likely outside this snippet, such as browser caching or other scripts.
Final Answer:
The event modifiers are correct; the problem is elsewhere. -> Option A
Quick Check:
Modifiers auto-call preventDefault and stopPropagation [OK]
Hint: Modifiers auto-call preventDefault; no manual call needed [OK]
Common Mistakes:
Thinking modifier order matters
Expecting manual event.preventDefault() call
Assuming missing event parameter causes issue
5. You want to create a nested clickable list in Svelte where clicking a child item logs 'Child clicked' and clicking the parent logs 'Parent clicked'. You want to prevent the parent's click when a child is clicked. Which code correctly uses event modifiers to achieve this?