0
0
Vueframework~20 mins

v-model with checkboxes in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Checkbox Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when selecting checkboxes bound with v-model array?
Consider this Vue component using v-model with checkboxes. What will be the value of selectedFruits after the user checks 'Apple' and 'Banana'?
Vue
<template>
  <div>
    <label><input type="checkbox" value="Apple" v-model="selectedFruits"> Apple</label>
    <label><input type="checkbox" value="Banana" v-model="selectedFruits"> Banana</label>
    <label><input type="checkbox" value="Cherry" v-model="selectedFruits"> Cherry</label>
  </div>
</template>

<script setup>
import { ref } from 'vue'
const selectedFruits = ref([])
</script>
A["Apple", "Banana"]
B["Apple"]
C["Banana"]
D[]
Attempts:
2 left
💡 Hint
Remember that v-model on checkboxes with the same array binds all checked values into that array.
📝 Syntax
intermediate
1:30remaining
Which option correctly binds a single checkbox with v-model to a boolean?
You want to bind a single checkbox to a boolean value using v-model. Which code snippet correctly achieves this?
A<input type="checkbox" v-model="isChecked" checked>
B<input type="checkbox" v-model="isChecked" value="true">
C<input type="checkbox" v-model="isChecked" :value="true">
D<input type="checkbox" v-model="isChecked">
Attempts:
2 left
💡 Hint
For a single checkbox, v-model binds to a boolean by default.
🔧 Debug
advanced
2:30remaining
Why does this checkbox group not update the array with v-model?
Given this Vue code, why does checking the boxes not update the selectedColors array?
Vue
<template>
  <div>
    <label><input type="checkbox" :value="color" v-model="selectedColors"> {{ color }}</label>
  </div>
</template>

<script setup>
import { ref } from 'vue'
const colors = ['Red', 'Green', 'Blue']
const selectedColors = ref([])
</script>
AThe template is missing a loop to render checkboxes for each color.
BThe v-model should be on the label, not the input.
CThe :value binding is incorrect; it should be a string literal.
DselectedColors should be a reactive object, not a ref.
Attempts:
2 left
💡 Hint
Check if all checkboxes are rendered for each color.
state_output
advanced
2:00remaining
What is the value of selectedOptions after toggling checkboxes?
In this Vue component, what is the value of selectedOptions after the user checks 'Option1', then 'Option3', then unchecks 'Option1'?
Vue
<template>
  <label><input type="checkbox" value="Option1" v-model="selectedOptions"> Option1</label>
  <label><input type="checkbox" value="Option2" v-model="selectedOptions"> Option2</label>
  <label><input type="checkbox" value="Option3" v-model="selectedOptions"> Option3</label>
</template>

<script setup>
import { ref } from 'vue'
const selectedOptions = ref([])
</script>
A[]
B["Option1", "Option3"]
C["Option3"]
D["Option2", "Option3"]
Attempts:
2 left
💡 Hint
Remember that unchecking removes the value from the array.
🧠 Conceptual
expert
3:00remaining
What error occurs if v-model is used on a checkbox without a value attribute in a group?
In Vue, when using multiple checkboxes bound to the same array with v-model, what happens if one checkbox lacks a value attribute?
Vue
<template>
  <label><input type="checkbox" v-model="selected" value="A"> A</label>
  <label><input type="checkbox" v-model="selected"> B</label> <!-- Missing value -->
</template>

<script setup>
import { ref } from 'vue'
const selected = ref([])
</script>
AVue throws a runtime error because value is required for checkboxes with array v-model.
BThe checkbox without value uses 'on' as its default value, adding 'on' to the array when checked.
CThe checkbox without value is ignored and never added to the array.
DThe checkbox without value binds as a boolean instead of string.
Attempts:
2 left
💡 Hint
Check the default value of a checkbox input in HTML when no value attribute is set.