v-model with radio buttons lets you easily link a group of radio buttons to a single value. This way, when you pick one option, Vue automatically updates the value for you.
v-model with radio buttons in Vue
<input type="radio" v-model="selectedValue" value="option1" /> <input type="radio" v-model="selectedValue" value="option2" />
The v-model directive binds the radio buttons to the same data property.
The value attribute on each radio button sets what value selectedValue will have when that button is chosen.
picked. Initially, 'A' is selected.<template> <input type="radio" v-model="picked" value="A" /> A <input type="radio" v-model="picked" value="B" /> B </template> <script setup> import { ref } from 'vue' const picked = ref('A') </script>
<template>
<label>
<input type="radio" v-model="choice" value="yes" /> Yes
</label>
<label>
<input type="radio" v-model="choice" value="no" /> No
</label>
<p>You picked: {{ choice }}</p>
</template>
<script setup>
import { ref } from 'vue'
const choice = ref('no')
</script>This component shows three radio buttons for fruits. The selected fruit is stored in fruit and displayed below. Initially, Banana is selected.
<template>
<h2>Pick your favorite fruit:</h2>
<label>
<input type="radio" v-model="fruit" value="Apple" /> Apple
</label>
<label>
<input type="radio" v-model="fruit" value="Banana" /> Banana
</label>
<label>
<input type="radio" v-model="fruit" value="Cherry" /> Cherry
</label>
<p>Your choice: {{ fruit }}</p>
</template>
<script setup>
import { ref } from 'vue'
const fruit = ref('Banana')
</script>Wrap radio buttons in <label> tags to improve accessibility and make clicking the text select the button.
Make sure all radio buttons bound with v-model share the same data property to work as a group.
v-model binds radio buttons to a single data value.
The value attribute sets what value is assigned when a radio button is selected.
Use labels for better user experience and accessibility.
Practice
v-model do when used with radio buttons in Vue?Solution
Step 1: Understand v-model binding
Thev-modeldirective in Vue binds form inputs to a data property, syncing the input's value with the data.Step 2: Apply to radio buttons
For radio buttons,v-modelbinds the selected button'svalueto a single data property, so only one value is stored.Final Answer:
It binds the selected radio button's value to a single data property. -> Option AQuick Check:
v-model syncs selected radio value = C [OK]
- Thinking each radio has its own data property
- Assuming v-model disables radios
- Believing v-model auto-submits forms
v-model to a data property named choice?Solution
Step 1: Identify correct directive usage
The correct directive to bind input value to data isv-model, notv-bindor:model.Step 2: Check attribute values
Thevalueattribute sets the radio's value, andv-model='choice'binds the selected value to thechoicedata property.Final Answer:
<input type='radio' v-model='choice' value='A'> -> Option DQuick Check:
Use v-model='choice' with value='A' = A [OK]
- Using v-bind instead of v-model
- Swapping value and v-model attributes
- Using incorrect directive like :model
<template>
<input type='radio' id='red' v-model='color' value='red'>
<label for='red'>Red</label>
<input type='radio' id='blue' v-model='color' value='blue'>
<label for='blue'>Blue</label>
<p>Selected color: {{ color }}</p>
</template>If the user clicks the radio button labeled "Blue", what will be displayed inside the paragraph?
Solution
Step 1: Understand v-model behavior on radio buttons
When a radio button is selected, itsvalueis assigned to the bound data property, herecolor.Step 2: Apply user action
User clicks the radio withvalue='blue', socolorbecomes 'blue'. The paragraph shows this value.Final Answer:
Selected color: blue -> Option CQuick Check:
Click blue radio sets color = 'blue' [OK]
- Assuming initial value stays after click
- Confusing value attribute with label text
- Expecting boolean true instead of string value
<input type='radio' v-model='selection' value='A'> <input type='radio' v-model='selection' value='B'> <input type='radio' v-model='selection' value='C'>
Why might none of the radio buttons appear selected even after clicking them?
Solution
Step 1: Check data initialization
Vue'sv-modelbinds to a data property. Ifselectionis undefined initially, no radio is selected by default.Step 2: Understand radio selection behavior
Clicking a radio setsselectionto that value, but if the component does not react or update, it may be due to missing reactive data or errors elsewhere.Final Answer:
The data propertyselectionis not initialized in the Vue component. -> Option BQuick Check:
Uninitialized data property causes no selection [OK]
- Thinking name attribute is required for v-model radios
- Ignoring data property initialization
- Assuming misspelled v-model without checking
selectedFruit with options 'Apple', 'Banana', and 'Cherry'. You also want the label text to be clickable for accessibility. Which of the following Vue template snippets correctly implements this?Solution
Step 1: Understand label accessibility
Wrapping theinputinside alabelmakes the label text clickable and improves accessibility without needingforandid.Step 2: Check the options
<div> <label> <input type='radio' v-model='selectedFruit' value='Apple'> Apple </label> <label> <input type='radio' v-model='selectedFruit' value='Banana'> Banana </label> <label> <input type='radio' v-model='selectedFruit' value='Cherry'> Cherry </label> </div>wraps each radio input inside a label with the text, which is the recommended accessible pattern.Final Answer:
The snippet wrapping inputs inside <label> tags for clickable labels. -> Option AQuick Check:
Wrap input in label for clickable text = B [OK]
- Using labels without for/id pairs
- Not wrapping input in label causing unclickable text
- Forgetting to bind v-model on each radio
Ready to prove it?
Pass the test and this pattern moves unknown
