📖 Scenario: You are building a simple preferences form for a website where users can select their favorite fruits using checkboxes.
🎯 Goal: Create a Vue component that uses v-model to bind an array of selected fruits to multiple checkboxes. The component should update the array automatically when checkboxes are checked or unchecked.
📋 What You'll Learn
Create a Vue component with a reactive array called selectedFruits initialized as empty.
Add a list of fruit names as checkboxes in the template.
Use v-model on each checkbox to bind it to selectedFruits.
Display the currently selected fruits below the checkboxes.
💡 Why This Matters
🌍 Real World
Checkbox groups are common in forms where users select multiple options, such as preferences, filters, or interests.
💼 Career
Understanding how to bind multiple checkboxes to a reactive array with v-model is essential for building interactive forms in Vue applications.
Progress0 / 4 steps
1
Set up the initial data array
Create a Vue component with a reactive array called selectedFruits initialized as an empty array [] inside the setup() function using ref.
Vue
Hint
Use import { ref } from 'vue' and then const selectedFruits = ref([]) inside <script setup>.
2
Add a list of fruit checkboxes
In the <template>, add three checkboxes with labels: Apple, Banana, and Cherry. Use input elements of type checkbox with value attributes set to the fruit names.
Vue
Hint
Use three input elements with type="checkbox" and value set to each fruit name.
3
Bind checkboxes with v-model
Add v-model="selectedFruits" to each checkbox input to bind them to the reactive array selectedFruits.
Vue
Hint
Add v-model="selectedFruits" to each checkbox input to link them to the reactive array.
4
Display selected fruits
Below the checkboxes, add a <div> that shows the text Selected fruits: followed by the joined list of fruits in selectedFruits using {{ selectedFruits.join(', ') }}.
Vue
Hint
Use {{ selectedFruits.join(', ') }} inside a <div> to display the selected fruits separated by commas.
Practice
(1/5)
1. What type of data should you use with v-model when binding to multiple checkboxes in Vue?
easy
A. A string representing the last selected checkbox
B. A boolean to represent true or false
C. An array to hold the selected values
D. A number counting how many checkboxes are checked
Solution
Step 1: Understand checkbox binding
When multiple checkboxes are bound with v-model, Vue expects an array to store all selected values.
Step 2: Compare data types for checkboxes
A boolean is used for single checkboxes, but for multiple checkboxes, an array collects all checked values.
Final Answer:
An array to hold the selected values -> Option C
Quick Check:
Multiple checkboxes = array [OK]
Hint: Multiple checkboxes always bind to an array [OK]
Common Mistakes:
Using a boolean for multiple checkboxes
Binding to a string instead of an array
Expecting a number to track checked boxes
2. Which of the following is the correct syntax to bind a single checkbox with v-model in Vue?
easy
A. <input type='checkbox' v-model='checked' />
B. <input type='checkbox' :value='checked' />
C. <input type='checkbox' v-bind='checked' />
D. <input type='checkbox' v-model:value='checked' />
Solution
Step 1: Identify correct binding directive
The v-model directive binds the checkbox's checked state to a data property.
Step 2: Check syntax correctness
<input type='checkbox' v-model='checked' /> uses v-model='checked' which is the correct and simplest syntax for binding a single checkbox.
Final Answer:
<input type='checkbox' v-model='checked' /> -> Option A
Quick Check:
Single checkbox uses v-model='property' [OK]
Hint: Use v-model directly on checkbox for single binding [OK]
Why does checking the box not add 'yes' to checked?
medium
A. Because checked should be a string, not boolean
B. Because value attribute is missing
C. Because v-model cannot be used on checkboxes
D. Because checked is a boolean, not an array, so value is ignored
Solution
Step 1: Identify data type for v-model
The variable checked is a boolean, so Vue treats the checkbox as a single toggle (true/false).
Step 2: Understand effect of value attribute
When v-model is boolean, the value attribute is ignored; it only matters when binding to an array.
Final Answer:
Because checked is a boolean, not an array, so value is ignored -> Option D
Quick Check:
Boolean v-model ignores value attribute [OK]
Hint: Boolean v-model ignores value; use array for multiple values [OK]
Common Mistakes:
Thinking value attribute always affects checkbox state
Believing v-model can't be used on checkboxes
Using string instead of boolean or array incorrectly
5. You want to create a group of checkboxes for selecting favorite fruits using v-model. The data property is favorites initialized as an empty array. Which code snippet correctly updates favorites when users check or uncheck fruits?
Step 1: Understand array binding for multiple checkboxes
Binding multiple checkboxes to the same array with v-model and distinct value attributes collects checked values in the array.
Step 2: Evaluate each option's correctness
<input type='checkbox' v-model='favorites' value='apple' />
<input type='checkbox' v-model='favorites' value='banana' />
<input type='checkbox' v-model='favorites' value='cherry' /> correctly uses v-model='favorites' with unique string values for each checkbox, so checked fruits are added or removed from the array.