Consider this Svelte component using group binding with checkboxes:
<script>
let selected = [];
</script>
<input type="checkbox" bind:group={selected} value="apple" /> Apple
<input type="checkbox" bind:group={selected} value="banana" /> Banana
<input type="checkbox" bind:group={selected} value="cherry" /> Cherry
<p>Selected: {selected.join(', ')}</p>If the user checks only the "banana" and "cherry" boxes, what will be displayed inside the <p> tag?
Group binding collects all checked values into the array.
Only the checked checkboxes add their values to the selected array. Since "banana" and "cherry" are checked, the output lists those two.
Given this Svelte snippet:
<script>
let fruits = ['apple'];
</script>
<input type="checkbox" bind:group={fruits} value="apple" /> Apple
<input type="checkbox" bind:group={fruits} value="banana" /> Banana
<input type="checkbox" bind:group={fruits} value="cherry" /> CherryInitially, only "apple" is checked. The user then checks "banana" and unchecks "apple". What is the value of fruits after these changes?
Group binding updates the array to include only checked values.
Unchecking "apple" removes it from the array. Checking "banana" adds it. "cherry" remains unchecked, so it is not included.
Choose the correct Svelte code snippet that binds a group of radio buttons to a variable color:
Radio buttons use bind:group to bind a single variable to the selected value.
Option A correctly binds the color variable to the selected radio button using bind:group. Option A uses invalid syntax. Option A binds to colors which is undefined. Option A uses checkboxes instead of radios.
Examine this Svelte code snippet:
<script>
let selected = '';
</script>
<input type="checkbox" bind:group={selected} value="one" /> One
<input type="checkbox" bind:group={selected} value="two" /> TwoWhen running, it throws an error. What is the cause?
Check the type of the variable used with bind:group for checkboxes.
Checkbox group binding requires the bound variable to be an array to hold multiple selected values. Using a string causes a runtime error.
In Svelte, if you bind a group of checkboxes to an array variable using bind:group, and then programmatically modify the array (e.g., add or remove values), what happens to the checkboxes?
Think about two-way binding and reactivity in Svelte.
Svelte's bind:group creates a two-way binding. Changing the array programmatically updates the checkboxes to match the array's contents automatically.