0
0
Svelteframework~20 mins

Select bindings in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Select Binding Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when selecting an option?

Consider this Svelte component with a select bound to selected. What will be the value of selected after choosing the option labeled "Two"?

Svelte
  <script>
    let selected = '1';
  </script>
  <select bind:value={selected}>
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
  </select>
A"2" (string)
B2 (number)
C"Two" (string label)
Dundefined
Attempts:
2 left
💡 Hint

Remember that HTML option values are always strings unless converted.

state_output
intermediate
2:00remaining
What is the value of the bound variable after multiple selections?

Given this Svelte code with a multiple select, what will be the value of selected after the user selects "Apple" and "Cherry"?

Svelte
  <script>
    let selected = [];
  </script>
  <select bind:value={selected} multiple>
    <option value="apple">Apple</option>
    <option value="banana">Banana</option>
    <option value="cherry">Cherry</option>
  </select>
A["Apple", "Cherry"]
B"apple,cherry"
C["apple", "cherry"]
Dundefined
Attempts:
2 left
💡 Hint

Multiple select bindings use arrays of strings matching option values.

📝 Syntax
advanced
2:00remaining
Which option correctly binds a select to a number variable?

You want to bind a select's value to a number variable count. Which code snippet correctly achieves this in Svelte?

A<script>let count = 1;</script><select bind:value={count}><option value={1}>One</option><option value={2}>Two</option></select>
B<script>let count = 1;</script><select bind:value={count}><option value="1">One</option><option value="2">Two</option></select>
C<script>let count = 1;</script><select bind:value={+count}><option value="1">One</option><option value="2">Two</option></select>
D<script>let count = 1;</script><select bind:value={count}><option value={"1"}>One</option><option value={"2"}>Two</option></select>
Attempts:
2 left
💡 Hint

Binding to a number requires option values to be numbers, not strings.

🔧 Debug
advanced
2:00remaining
Why does this multiple select binding not update the variable?

Given this Svelte code, why does selected remain empty after selecting options?

Svelte
  <script>
    let selected = [];
  </script>
  <select bind:value={selected}>
    <option value="a">A</option>
    <option value="b">B</option>
    <option value="c">C</option>
  </select>
ABecause the <code>bind:value</code> syntax is incorrect and should be <code>bind:selected</code>.
BBecause the variable <code>selected</code> is initialized as an array instead of a string.
CBecause option values must be numbers for binding to work.
DBecause the <code>multiple</code> attribute is missing on the <code>select</code> element.
Attempts:
2 left
💡 Hint

Multiple selections require a special attribute on the select element.

🧠 Conceptual
expert
3:00remaining
How does Svelte handle select binding with complex objects?

In Svelte, if you bind a select to a variable and use option elements with value set to objects, what happens when you select an option?

Svelte
  <script>
    let selected;
    const options = [
      { id: 1, name: 'One' },
      { id: 2, name: 'Two' }
    ];
  </script>
  <select bind:value={selected}>
    {#each options as option}
      <option value={option}>{option.name}</option>
    {/each}
  </select>
AThe variable <code>selected</code> will be undefined because objects cannot be serialized.
BThe variable <code>selected</code> will be the exact object selected from the options array.
CSvelte throws a runtime error because option values cannot be objects.
DThe variable <code>selected</code> will be a string "[object Object]" regardless of selection.
Attempts:
2 left
💡 Hint

Svelte supports binding non-primitive values like objects in select options using strict equality.