Bird
Raised Fist0
Svelteframework~20 mins

svelte:body for body events - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Svelte Body Event Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
1:30remaining
What happens when you use svelte:body to listen for a click event?
Consider this Svelte component code:
<script>
  let count = 0;
  function handleClick() {
    count += 1;
  }
</script>

<svelte:body on:click={handleClick} />

<p>Body clicked {count} times.</p>

What will the paragraph display after clicking anywhere on the page body three times?
Svelte
<script>
  let count = 0;
  function handleClick() {
    count += 1;
  }
</script>

<svelte:body on:click={handleClick} />

<p>Body clicked {count} times.</p>
ABody clicked 3 times.
BBody clicked 1 times.
CBody clicked 0 times.
DBody clicked NaN times.
Attempts:
2 left
💡 Hint
Remember that svelte:body listens to events on the whole document body, not just inside the component.
📝 Syntax
intermediate
1:00remaining
Which option correctly attaches a keydown event listener to the body using svelte:body?
You want to listen for the 'keydown' event on the whole page body in a Svelte component. Which of the following code snippets is syntactically correct?
A<svelte:body on-keydown={handleKeydown} />
B<svelte:body onKeydown={handleKeydown} />
C<svelte:body bind:keydown={handleKeydown} />
D<svelte:body on:keydown={handleKeydown} />
Attempts:
2 left
💡 Hint
Svelte uses the syntax on:eventname for event listeners.
🔧 Debug
advanced
2:00remaining
Why does this svelte:body event listener not update the UI?
Given this code:
<script>
  let pressed = { pressed: false };
  function onKeydown() {
    pressed.pressed = true;
  }
</script>

<svelte:body on:keydown={onKeydown} />

<p>Key pressed: {pressed.pressed}</p>

Pressing any key does not update the displayed text. Why?
Svelte
<script>
  let pressed = { pressed: false };
  function onKeydown() {
    pressed.pressed = true;
  }
</script>

<svelte:body on:keydown={onKeydown} />

<p>Key pressed: {pressed.pressed}</p>
AThe <code>svelte:body</code> tag cannot listen to keydown events.
BThe event handler changes the variable but does not trigger Svelte's reactivity because the variable is not reassigned properly.
CThe variable <code>pressed</code> is not declared with <code>let</code>.
DThe event handler function is missing the event parameter.
Attempts:
2 left
💡 Hint
Svelte updates the UI only when reactive variables are reassigned.
🧠 Conceptual
advanced
1:30remaining
What is the main difference between svelte:body and adding event listeners directly to window in Svelte?
Choose the best explanation for how svelte:body differs from adding event listeners directly to window in a Svelte component.
A<code>svelte:body</code> can only listen to click events, but <code>window</code> can listen to any event.
B<code>svelte:body</code> listens only to events inside the component, while <code>window</code> listeners catch all events globally.
C<code>svelte:body</code> automatically cleans up event listeners when the component unmounts, while manually added <code>window</code> listeners require manual cleanup.
D<code>svelte:body</code> attaches listeners to the document head, while <code>window</code> attaches to the browser window.
Attempts:
2 left
💡 Hint
Think about lifecycle and cleanup of event listeners.
❓ state_output
expert
2:30remaining
What is the value of count after this sequence of events?
Given this Svelte component:
<script>
  let count = 0;
  function increment() {
    count += 1;
  }
  function decrement() {
    count -= 1;
  }
</script>

<svelte:body on:click={increment} on:contextmenu|preventDefault={decrement} />

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

What is the value of count after the user performs these actions in order?
1. Left-click anywhere on the page body
2. Right-click anywhere on the page body
3. Left-click anywhere on the page body
Svelte
<script>
  let count = 0;
  function increment() {
    count += 1;
  }
  function decrement() {
    count -= 1;
  }
</script>

<svelte:body on:click={increment} on:contextmenu|preventDefault={decrement} />

<p>Count: {count}</p>
A1
B2
C3
D-1
Attempts:
2 left
💡 Hint
Remember that right-click triggers the contextmenu event and the preventDefault modifier stops the menu but still runs the handler.

Practice

(1/5)
1. What is the main purpose of using svelte:body in a Svelte component?
easy
A. To listen for events on the entire page's body element.
B. To style the body element with CSS.
C. To create a new body element inside the component.
D. To replace the default body element with a custom one.

Solution

  1. Step 1: Understand the role of svelte:body

    svelte:body is a special tag in Svelte that allows you to listen to events on the whole page's body element.
  2. Step 2: Compare options with this purpose

    Only To listen for events on the entire page's body element. correctly states that it listens for events on the entire page's body element. Other options describe styling or replacing the body, which svelte:body does not do.
  3. Final Answer:

    To listen for events on the entire page's body element. -> Option A
  4. Quick Check:

    svelte:body listens to body events = A [OK]
Hint: Remember: svelte:body catches events on the whole page body [OK]
Common Mistakes:
  • Thinking svelte:body styles or replaces the body element
  • Confusing svelte:body with component-level event listeners
  • Assuming it creates a new body element
2. Which of the following is the correct syntax to listen for a click event on the body using svelte:body?
easy
A. <body on:click={handleClick}></body>
B. <svelte:body on:click={handleClick} />
C. <div on:bodyclick={handleClick}></div>
D. <svelte:window on:click={handleClick} />

Solution

  1. Step 1: Identify correct event listening syntax for body

    In Svelte, svelte:body is used to listen to events on the body element. The syntax is <svelte:body on:event={handler} />.
  2. Step 2: Compare options

    <svelte:body on:click={handleClick} /> uses <svelte:body on:click={handleClick} />, which is correct. <body on:click={handleClick}></body> tries to add event on a normal body tag inside component, which is invalid. <div on:bodyclick={handleClick}></div> uses a non-existent event bodyclick. <svelte:window on:click={handleClick} /> listens on svelte:window, not svelte:body.
  3. Final Answer:

    <svelte:body on:click={handleClick} /> -> Option B
  4. Quick Check:

    Correct syntax for body event = A [OK]
Hint: Use for body events [OK]
Common Mistakes:
  • Using <body> tag inside component for events
  • Confusing svelte:body with svelte:window
  • Using wrong event names like bodyclick
3. Given this Svelte code snippet, what will be logged when you press any key on the keyboard?
<script>
  function handleKey(event) {
    console.log(event.key);
  }
</script>

<svelte:body on:keydown={handleKey} />
medium
A. Nothing will be logged because svelte:body does not handle key events.
B. An error because handleKey is not defined.
C. The key name of the pressed key (e.g., 'a', 'Enter').
D. The string 'keydown' will be logged.

Solution

  1. Step 1: Understand event handling with svelte:body

    The svelte:body tag listens to events on the page's body. Here, it listens for keydown events and calls handleKey.
  2. Step 2: Analyze the handler function

    handleKey logs event.key, which is the name of the key pressed (like 'a', 'Enter').
  3. Final Answer:

    The key name of the pressed key (e.g., 'a', 'Enter'). -> Option C
  4. Quick Check:

    keydown event logs event.key = B [OK]
Hint: event.key holds the pressed key name in keyboard events [OK]
Common Mistakes:
  • Thinking svelte:body can't listen to keyboard events
  • Expecting a fixed string instead of event.key
  • Assuming handleKey is undefined
4. What is wrong with this Svelte code snippet that tries to listen for clicks on the body?
<script>
  function clickHandler() {
    alert('Body clicked');
  }
</script>

<svelte:body onclick={clickHandler} />
medium
A. The alert function is not allowed inside event handlers.
B. The function clickHandler must be async.
C. You cannot use svelte:body for click events.
D. The event name should be on:click, not onclick.

Solution

  1. Step 1: Check event binding syntax in Svelte

    Svelte requires event listeners to use the syntax on:eventName, for example on:click. Using onclick is invalid.
  2. Step 2: Verify other parts

    The function does not need to be async, svelte:body supports click events, and alert is allowed in handlers.
  3. Final Answer:

    The event name should be on:click, not onclick. -> Option D
  4. Quick Check:

    Use on:click, not onclick in Svelte [OK]
Hint: Use on:event, not onclick, for Svelte event handlers [OK]
Common Mistakes:
  • Writing onclick instead of on:click
  • Thinking handlers must be async
  • Believing svelte:body can't handle clicks
5. You want to close a dropdown menu when the user clicks anywhere outside it. Which approach using svelte:body is best to achieve this behavior?
hard
A. Use <svelte:body on:click={handleClickOutside} /> and check if the click target is outside the dropdown element.
B. Add on:click directly on the dropdown element only.
C. Use <svelte:window on:click={closeDropdown} /> without checking the click target.
D. Listen for click events on a parent div wrapping the dropdown.

Solution

  1. Step 1: Understand the problem

    To close a dropdown when clicking outside it, you need to detect clicks anywhere on the page and check if the click was outside the dropdown.
  2. Step 2: Evaluate options

    Use <svelte:body on:click={handleClickOutside} /> and check if the click target is outside the dropdown element. uses svelte:body to listen globally and then checks if the click target is outside the dropdown, which is the correct approach. Add on:click directly on the dropdown element only. only listens on the dropdown itself, so clicks outside won't be detected. Use <svelte:window on:click={closeDropdown} /> without checking the click target. listens on window but doesn't check target, so it closes even when clicking inside. Listen for click events on a parent div wrapping the dropdown. listens on a parent div, which may not cover the whole page.
  3. Final Answer:

    Use <svelte:body on:click={handleClickOutside} /> and check if the click target is outside the dropdown element. -> Option A
  4. Quick Check:

    Global body click with target check = C [OK]
Hint: Use svelte:body click and check target outside dropdown [OK]
Common Mistakes:
  • Listening only on dropdown element
  • Not checking if click is outside dropdown
  • Using window without target check
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard