Checkbox and radio bindings let you easily connect form inputs to your code. This means your app knows what the user selects without extra work.
Checkbox and radio bindings in Svelte
<input type="checkbox" bind:checked={variable} /> <input type="radio" bind:group={variable} value="option1" />
Use bind:checked for checkboxes to link a boolean variable.
Use bind:group for radio buttons to link a variable that holds the selected value.
isChecked. The text updates as you check or uncheck.<script> let isChecked = false; </script> <input type="checkbox" bind:checked={isChecked} /> <p>Checked? {isChecked ? 'Yes' : 'No'}</p>
selectedColor. Only one can be selected at a time.<script> let selectedColor = ''; </script> <label><input type="radio" bind:group={selectedColor} value="red" /> Red</label> <label><input type="radio" bind:group={selectedColor} value="blue" /> Blue</label> <p>Selected color: {selectedColor}</p>
bind:group can link to an array to track multiple selections.<script> let fruits = []; </script> <label><input type="checkbox" bind:group={fruits} value="apple" /> Apple</label> <label><input type="checkbox" bind:group={fruits} value="banana" /> Banana</label> <p>Fruits selected: {fruits.join(', ')}</p>
This component shows three examples: a single checkbox, radio buttons, and multiple checkboxes bound to variables. The displayed text updates as you interact.
<script> let agree = false; let gender = ''; let hobbies = []; </script> <main> <h2>Checkbox binding</h2> <label> <input type="checkbox" bind:checked={agree} /> I agree to terms </label> <p>Agreed? {agree ? 'Yes' : 'No'}</p> <h2>Radio binding</h2> <label><input type="radio" bind:group={gender} value="male" /> Male</label> <label><input type="radio" bind:group={gender} value="female" /> Female</label> <p>Selected gender: {gender}</p> <h2>Multiple checkboxes with group</h2> <label><input type="checkbox" bind:group={hobbies} value="reading" /> Reading</label> <label><input type="checkbox" bind:group={hobbies} value="sports" /> Sports</label> <label><input type="checkbox" bind:group={hobbies} value="music" /> Music</label> <p>Hobbies: {hobbies.length > 0 ? hobbies.join(', ') : 'None'}</p> </main>
Checkbox bind:checked links to a boolean (true/false).
Radio buttons use bind:group to share one variable for the selected value.
Multiple checkboxes can share bind:group with an array to track all checked items.
Checkboxes use bind:checked for true/false values.
Radio buttons use bind:group to select one value from many.
Multiple checkboxes can bind to an array with bind:group to track all selections.
Practice
Solution
Step 1: Understand checkbox binding
Checkboxes in Svelte usebind:checkedto track true/false state.Step 2: Differentiate from other bindings
bind:valueis for input values,bind:groupis for radio or multiple checkboxes,bind:selectedis invalid.Final Answer:
<code>bind:checked</code> -> Option CQuick Check:
Checkbox true/false = bind:checked [OK]
- Using bind:value for checkbox state
- Confusing bind:group with bind:checked
- Trying bind:selected which doesn't exist
color in Svelte?Solution
Step 1: Identify radio button binding
Radio buttons usebind:groupto bind multiple inputs to one variable.Step 2: Check syntax correctness
Onlybind:group={color}correctly binds the group to variablecolor.Final Answer:
<input type='radio' bind:group={color} value='red' /> -> Option DQuick Check:
Radio group binding = bind:group [OK]
- Using bind:checked for radio groups
- Using bind:value instead of bind:group
- Using bind:selected which is invalid
<script>
let selectedColors = [];
</script>
<input type='checkbox' bind:group={selectedColors} value='red' /> Red
<input type='checkbox' bind:group={selectedColors} value='blue' /> Blue
<input type='checkbox' bind:group={selectedColors} value='green' /> Green
<p>Selected: {JSON.stringify(selectedColors)}</p>If the user checks 'red' and 'green', what will be displayed inside the paragraph?
Solution
Step 1: Understand bind:group with checkboxes
Binding multiple checkboxes withbind:groupstores selected values in an array.Step 2: Check which values are selected
User selects 'red' and 'green', so array contains ["red", "green"].Final Answer:
Selected: ["red", "green"] -> Option AQuick Check:
Checkbox group array = ["red", "green"] [OK]
- Expecting boolean array instead of values
- Confusing bind:checked with bind:group
- Outputting string instead of JSON array
<script>
let fruit = 'apple';
</script>
<input type='radio' bind:checked={fruit} value='apple' /> Apple
<input type='radio' bind:group={fruit} value='banana' /> Banana
What is the main issue with this code?
Solution
Step 1: Identify bindings on radio buttons
One radio usesbind:checked, the other usesbind:groupon the same variable.Step 2: Understand correct radio binding
Radio buttons must all usebind:groupto bind to one variable consistently.Final Answer:
Mixing bind:checked and bind:group on radio buttons causes inconsistent binding -> Option BQuick Check:
Radio group must use bind:group consistently [OK]
- Using bind:checked on radios instead of bind:group
- Mixing different bindings on radios
- Assuming bind:value binds radio group
selectedToppings and the size to a string pizzaSize?Solution
Step 1: Bind multiple checkboxes to an array
Usebind:groupon checkboxes to bind selected values to an array variable.Step 2: Bind radio buttons to a string variable
Usebind:groupon radio buttons to bind the selected value to a string variable.Step 3: Verify correct syntax in options
<script> let selectedToppings = []; let pizzaSize = ''; </script> <input type='checkbox' bind:group={selectedToppings} value='pepperoni' /> Pepperoni <input type='checkbox' bind:group={selectedToppings} value='mushrooms' /> Mushrooms <input type='radio' bind:group={pizzaSize} value='small' /> Small <input type='radio' bind:group={pizzaSize} value='large' /> Largecorrectly usesbind:groupfor both checkboxes and radios with proper variables.Final Answer:
Option A code snippet -> Option AQuick Check:
Checkbox array + radio string = bind:group [OK]
- Using bind:checked for multiple checkboxes array
- Using bind:checked for radio groups
- Mixing bind:value and bind:checked incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
