v-model with checkboxes helps you easily link checkbox inputs to your data. It keeps your data and checkboxes in sync without extra code.
v-model with checkboxes in Vue
<input type="checkbox" v-model="dataProperty" :value="optionValue" />
If v-model is bound to a boolean, the checkbox is checked when true and unchecked when false.
If v-model is bound to an array, the checkbox adds or removes its value from the array automatically.
isChecked is true.<input type="checkbox" v-model="isChecked" />
selectedFruits. Checking adds the fruit to the array.<input type="checkbox" v-model="selectedFruits" value="apple" /> <input type="checkbox" v-model="selectedFruits" value="banana" />
This Vue component shows three checkboxes for fruits. The selectedFruits array updates automatically as you check or uncheck boxes. The selected fruits are displayed below.
<template>
<div>
<h2>Select your favorite fruits:</h2>
<label>
<input type="checkbox" v-model="selectedFruits" value="Apple" /> Apple
</label>
<label>
<input type="checkbox" v-model="selectedFruits" value="Banana" /> Banana
</label>
<label>
<input type="checkbox" v-model="selectedFruits" value="Cherry" /> Cherry
</label>
<p>You selected: {{ selectedFruits.length ? selectedFruits.join(', ') : 'None' }}</p>
</div>
</template>
<script setup>
import { ref } from 'vue'
const selectedFruits = ref([])
</script>When using v-model with checkboxes and arrays, the checkbox's value attribute is important to identify what gets added or removed.
For a single checkbox bound to a boolean, no value attribute is needed.
Remember to initialize your data property properly: boolean for single checkbox, array for multiple checkboxes.
v-model with checkboxes links checkbox inputs to your data easily.
Use a boolean for single checkboxes, and an array for multiple checkboxes.
Checkboxes update your data automatically as users interact with them.
Practice
v-model when binding to multiple checkboxes in Vue?Solution
Step 1: Understand checkbox binding
When multiple checkboxes are bound withv-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 CQuick Check:
Multiple checkboxes = array [OK]
- Using a boolean for multiple checkboxes
- Binding to a string instead of an array
- Expecting a number to track checked boxes
v-model in Vue?Solution
Step 1: Identify correct binding directive
Thev-modeldirective binds the checkbox's checked state to a data property.Step 2: Check syntax correctness
<input type='checkbox' v-model='checked' />usesv-model='checked'which is the correct and simplest syntax for binding a single checkbox.Final Answer:
<input type='checkbox' v-model='checked' /> -> Option AQuick Check:
Single checkbox uses v-model='property' [OK]
- Using :value instead of v-model
- Using v-bind without specifying property
- Using v-model:value which is invalid
<template>
<input type='checkbox' v-model='selected' value='apple' />
<input type='checkbox' v-model='selected' value='banana' />
<input type='checkbox' v-model='selected' value='cherry' />
<p>Selected: {{ selected }}</p>
</template>
<script setup>
import { ref } from 'vue'
const selected = ref([])
</script>What will be displayed after the user checks the boxes for "apple" and "cherry"?
Solution
Step 1: Understand v-model with multiple checkboxes
When multiple checkboxes share the samev-modelbound to an array, Vue adds the checkbox'svalueto the array when checked.Step 2: Check which boxes are selected
User checked "apple" and "cherry", so the array contains ['apple', 'cherry'].Final Answer:
Selected: ['apple', 'cherry'] -> Option BQuick Check:
Checked boxes values collected in array [OK]
- Expecting a string instead of an array
- Thinking only last checked value is stored
- Confusing boolean with array for multiple checkboxes
<input type='checkbox' v-model='checked' value='yes' />
<script setup>
import { ref } from 'vue'
const checked = ref(false)
</script>Why does checking the box not add 'yes' to
checked?Solution
Step 1: Identify data type for v-model
The variablecheckedis a boolean, so Vue treats the checkbox as a single toggle (true/false).Step 2: Understand effect of value attribute
Whenv-modelis boolean, thevalueattribute 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 DQuick Check:
Boolean v-model ignores value attribute [OK]
- Thinking value attribute always affects checkbox state
- Believing v-model can't be used on checkboxes
- Using string instead of boolean or array incorrectly
v-model. The data property is favorites initialized as an empty array. Which code snippet correctly updates favorites when users check or uncheck fruits?Solution
Step 1: Understand array binding for multiple checkboxes
Binding multiple checkboxes to the same array withv-modeland distinctvalueattributes 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 usesv-model='favorites'with unique string values for each checkbox, so checked fruits are added or removed from the array.Final Answer:
<input type='checkbox' v-model='favorites' value='apple' /> <input type='checkbox' v-model='favorites' value='banana' /> <input type='checkbox' v-model='favorites' value='cherry' /> -> Option AQuick Check:
Multiple checkboxes + array + unique values = correct binding [OK]
- Using boolean values instead of unique strings
- Binding to object properties instead of array
- Using same value for all checkboxes
Ready to prove it?
Pass the test and this pattern moves unknown
