Bird
Raised Fist0
Vueframework~10 mins

v-model with checkboxes in Vue - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to bind the checkbox to the data property using v-model.

Vue
<template>
  <input type="checkbox" [1]="checked" />
</template>

<script setup>
import { ref } from 'vue'
const checked = ref(false)
</script>
Drag options to blanks, or click blank then click option'
Av-bind
Bv-if
Cv-model
Dv-for
Attempts:
3 left
💡 Hint
Common Mistakes
Using v-bind instead of v-model causes one-way binding only.
Using v-if or v-for here does not bind the checkbox state.
2fill in blank
medium

Complete the code to bind multiple checkboxes to an array using v-model.

Vue
<template>
  <input type="checkbox" value="apple" [1]="fruits" /> Apple
  <input type="checkbox" value="banana" [1]="fruits" /> Banana
</template>

<script setup>
import { ref } from 'vue'
const fruits = ref([])
</script>
Drag options to blanks, or click blank then click option'
Av-bind
Bv-if
Cv-show
Dv-model
Attempts:
3 left
💡 Hint
Common Mistakes
Using v-bind does not update the array on user clicks.
Using v-if or v-show does not bind the checkbox values.
3fill in blank
hard

Fix the error in the code by completing the directive to correctly bind the checkbox group.

Vue
<template>
  <input type="checkbox" value="red" [1]="colors" /> Red
  <input type="checkbox" value="blue" [1]="colors" /> Blue
</template>

<script setup>
import { ref } from 'vue'
const colors = ref(['red'])
</script>
Drag options to blanks, or click blank then click option'
Av-model
Bv-bind
Cv-if
Dv-show
Attempts:
3 left
💡 Hint
Common Mistakes
Using v-bind causes the checkboxes not to update the array.
Using v-if or v-show does not bind the checkbox values.
4fill in blank
hard

Fill both blanks to create a checkbox group bound to an array and display the selected items.

Vue
<template>
  <input type="checkbox" value="cat" [1]="pets" /> Cat
  <input type="checkbox" value="dog" [1]="pets" /> Dog
  <p>Selected pets: [2]</p>
</template>

<script setup>
import { ref } from 'vue'
const pets = ref([])
</script>
Drag options to blanks, or click blank then click option'
Av-model
Bpets.join(', ')
Cpets
Dv-bind
Attempts:
3 left
💡 Hint
Common Mistakes
Using v-bind instead of v-model prevents updating the array.
Displaying the array directly shows it as an object, not a readable list.
5fill in blank
hard

Fill all three blanks to bind checkboxes to an array, display count, and disable a checkbox conditionally.

Vue
<template>
  <input type="checkbox" value="java" [1]="languages" :disabled="languages.length >= 2 && !languages.includes('java')" /> Java
  <input type="checkbox" value="python" [1]="languages" :disabled="languages.length >= 2 && !languages.includes('python')" /> Python
  <input type="checkbox" value="js" [1]="languages" :disabled="languages.length >= 2 && !languages.includes('js')" /> JavaScript
  <p>Selected count: [2]</p>
  <p>Selected languages: [3]</p>
</template>

<script setup>
import { ref } from 'vue'
const languages = ref([])
</script>
Drag options to blanks, or click blank then click option'
Av-model
Blanguages.length
Clanguages.join(', ')
Dv-bind
Attempts:
3 left
💡 Hint
Common Mistakes
Using v-bind disables two-way binding.
Displaying the array directly shows an object, not a readable list.
Not disabling checkboxes allows selecting more than two.

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

  1. Step 1: Understand checkbox binding

    When multiple checkboxes are bound with v-model, Vue expects an array to store all selected values.
  2. Step 2: Compare data types for checkboxes

    A boolean is used for single checkboxes, but for multiple checkboxes, an array collects all checked values.
  3. Final Answer:

    An array to hold the selected values -> Option C
  4. 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

  1. Step 1: Identify correct binding directive

    The v-model directive binds the checkbox's checked state to a data property.
  2. 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.
  3. Final Answer:

    <input type='checkbox' v-model='checked' /> -> Option A
  4. Quick Check:

    Single checkbox uses v-model='property' [OK]
Hint: Use v-model directly on checkbox for single binding [OK]
Common Mistakes:
  • Using :value instead of v-model
  • Using v-bind without specifying property
  • Using v-model:value which is invalid
3. Given this Vue template code:
<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"?
medium
A. Selected: 'apple,cherry'
B. Selected: ['apple', 'cherry']
C. Selected: ['apple']
D. Selected: true

Solution

  1. Step 1: Understand v-model with multiple checkboxes

    When multiple checkboxes share the same v-model bound to an array, Vue adds the checkbox's value to the array when checked.
  2. Step 2: Check which boxes are selected

    User checked "apple" and "cherry", so the array contains ['apple', 'cherry'].
  3. Final Answer:

    Selected: ['apple', 'cherry'] -> Option B
  4. Quick Check:

    Checked boxes values collected in array [OK]
Hint: Multiple checkboxes with same v-model store values in array [OK]
Common Mistakes:
  • Expecting a string instead of an array
  • Thinking only last checked value is stored
  • Confusing boolean with array for multiple checkboxes
4. What is wrong with this Vue checkbox binding?
<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?
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

  1. 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).
  2. 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.
  3. Final Answer:

    Because checked is a boolean, not an array, so value is ignored -> Option D
  4. 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?
hard
A. <input type='checkbox' v-model='favorites' value='apple' /> <input type='checkbox' v-model='favorites' value='banana' /> <input type='checkbox' v-model='favorites' value='cherry' />
B. <input type='checkbox' v-model='favorites' :value='true' /> <input type='checkbox' v-model='favorites' :value='false' />
C. <input type='checkbox' v-model='favorites.apple' /> <input type='checkbox' v-model='favorites.banana' />
D. <input type='checkbox' v-model='favorites' value='true' /> <input type='checkbox' v-model='favorites' value='false' />

Solution

  1. 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.
  2. 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.
  3. 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 A
  4. Quick Check:

    Multiple checkboxes + array + unique values = correct binding [OK]
Hint: Use array with unique values for multiple checkboxes [OK]
Common Mistakes:
  • Using boolean values instead of unique strings
  • Binding to object properties instead of array
  • Using same value for all checkboxes
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard