Bird
Raised Fist0
Svelteframework~10 mins

Why events drive user interaction in Svelte - Test Your Understanding

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to listen for a click event on a button.

Svelte
<button on:[1]={() => alert('Clicked!')}>Click me</button>
Drag options to blanks, or click blank then click option'
Aclick
Bhover
Csubmit
Dchange
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'hover' instead of 'click' will not trigger on button press.
Using 'submit' is for forms, not buttons.
Using 'change' is for input fields, not buttons.
2fill in blank
medium

Complete the code to update a variable when the input changes.

Svelte
<input type="text" bind:value=[1]>
Drag options to blanks, or click blank then click option'
Atext
Bname
CinputValue
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'value' directly without declaring it.
Using 'text' which is not defined.
Using 'name' which is unrelated here.
3fill in blank
hard

Fix the error in the event handler syntax.

Svelte
<button on:click=[1]>Submit</button>
Drag options to blanks, or click blank then click option'
AhandleSubmit()
BhandleSubmit
ChandleSubmit();
DhandleSubmit =>
Attempts:
3 left
💡 Hint
Common Mistakes
Adding parentheses calls the function on render, not on event.
Using arrow syntax incorrectly inside the attribute.
Adding semicolons inside the attribute causes syntax errors.
4fill in blank
hard

Fill both blanks to create a button that increments a count on click.

Svelte
<script>
  let count = 0;
  function [1]() {
    count [2] 1;
  }
</script>
<button on:click={increment}>Clicked {count} times</button>
Drag options to blanks, or click blank then click option'
Aincrement
B+=
C-=
D++
Attempts:
3 left
💡 Hint
Common Mistakes
Using '--' or '-=' decreases the count.
Using '++' alone is not valid syntax in this context.
Mismatching function name and event handler causes errors.
5fill in blank
hard

Fill all three blanks to create an input that updates a message and shows it.

Svelte
<script>
  let [1] = '';
</script>
<input type="text" bind:value=[2] placeholder="Type here">
<p>{ [3] }</p>
Drag options to blanks, or click blank then click option'
Amessage
Dtext
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names causes the display not to update.
Using 'text' instead of 'message' breaks the binding.
Not binding the input value means the variable won't update.

Practice

(1/5)
1. In Svelte, what does the on:click directive do?
easy
A. It listens for a click event and runs the specified code when clicked.
B. It styles the element to look clickable.
C. It disables the element from being clicked.
D. It automatically submits a form when clicked.

Solution

  1. Step 1: Understand the purpose of on:click

    The on:click directive in Svelte listens for when the user clicks on an element.
  2. Step 2: What happens when clicked

    When the element is clicked, the code inside the event handler runs, allowing the app to respond.
  3. Final Answer:

    It listens for a click event and runs the specified code when clicked. -> Option A
  4. Quick Check:

    Event listener = on:click runs code on click [OK]
Hint: Remember: on:eventName runs code when event happens [OK]
Common Mistakes:
  • Thinking on:click changes styles automatically
  • Assuming on:click disables the element
  • Believing on:click submits forms by default
2. Which of the following is the correct syntax to handle a submit event on a form in Svelte?
easy
A.
B.
C.
D.

Solution

  1. Step 1: Recall Svelte event syntax

    Svelte uses on:eventName to listen to events, so on:submit is correct.
  2. Step 2: Check other options

    Options A, B, and D use incorrect syntax not supported by Svelte.
  3. Final Answer:

    <form on:submit={handleSubmit}> -> Option C
  4. Quick Check:

    Svelte event syntax = on:eventName [OK]
Hint: Use on:eventName={handler} for events in Svelte [OK]
Common Mistakes:
  • Using camelCase like onSubmit instead of on:submit
  • Using @submit which is Vue syntax
  • Placing 'on' after event name
3. What will be the output when the button below is clicked?
<script>
  let count = 0;
  function increment() {
    count += 1;
  }
</script>
<button on:click={increment}>Clicked {count} times</button>
medium
A. The button text updates to show how many times it was clicked.
B. The button text never changes.
C. The page reloads on each click.
D. An error occurs because count is not reactive.

Solution

  1. Step 1: Understand reactivity of count

    In Svelte, variables declared with let are reactive, so updating count updates the UI.
  2. Step 2: What happens on click

    Clicking the button calls increment(), which adds 1 to count, updating the displayed text.
  3. Final Answer:

    The button text updates to show how many times it was clicked. -> Option A
  4. Quick Check:

    Reactive variable update = UI changes [OK]
Hint: Svelte updates UI automatically when reactive variables change [OK]
Common Mistakes:
  • Thinking count is not reactive without $ prefix
  • Expecting page reload on click
  • Assuming button text stays static
4. Identify the error in this Svelte code snippet:
<script>
  let name = '';
  function updateName(event) {
    name = event.target.value;
  }
</script>
<input type="text" on:input={updateName} />
<p>Hello, {name}!</p>
medium
A. The event handler should be on:change, not on:input.
B. There is no error; the code updates name as the user types.
C. The input element is missing a bind:value directive.
D. The function updateName should use event.currentTarget.value instead of event.target.value.

Solution

  1. Step 1: Check event handler usage

    The on:input event fires whenever the user types, which is appropriate here.
  2. Step 2: Verify event property usage

    Using event.target.value correctly accesses the input's current value.
  3. Final Answer:

    There is no error; the code updates name as the user types. -> Option B
  4. Quick Check:

    on:input with event.target.value updates state correctly [OK]
Hint: on:input updates live; event.target.value is correct [OK]
Common Mistakes:
  • Confusing on:input with on:change
  • Thinking event.currentTarget is required
  • Believing bind:value is mandatory for updates
5. You want to create a Svelte component that counts clicks but only updates the count if the Shift key is held during the click. Which event handler code correctly implements this?
hard
A. { if(event.key === 'Shift') count++ }}>Click me
B. Click me
C. { if(count.shiftKey) count += 1 }}>Click me
D. { if(event.shiftKey) count += 1 }}>Click me

Solution

  1. Step 1: Check event object usage

    The event object has a shiftKey boolean indicating if Shift was pressed during the click.
  2. Step 2: Verify correct syntax and logic

    { if(event.shiftKey) count += 1 }}>Click me uses an arrow function with a condition checking event.shiftKey before incrementing count, which is correct.
  3. Step 3: Identify errors in other options

    Click me misuses expression syntax; { if(count.shiftKey) count += 1 }}>Click me incorrectly accesses count.shiftKey; { if(event.key === 'Shift') count++ }}>Click me checks event.key which is undefined on click events.
  4. Final Answer:

    <button on:click={event => { if(event.shiftKey) count += 1 }}>Click me</button> -> Option D
  5. Quick Check:

    Use event.shiftKey in click handler to check Shift key [OK]
Hint: Use event.shiftKey inside on:click handler to detect Shift key [OK]
Common Mistakes:
  • Checking event.key on click event (only for keyboard events)
  • Trying to access shiftKey on count variable
  • Incorrect arrow function syntax in event handler
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