📖 Scenario: You are building a simple survey form in Vue where users select their favorite fruit using radio buttons.
🎯 Goal: Create a Vue component that uses v-model to bind a group of radio buttons to a data property. The selected fruit should update reactively.
📋 What You'll Learn
Create a reactive data property called favoriteFruit with an initial empty string.
Add a configuration variable fruits as an array of fruit names.
Use v-for to render radio buttons for each fruit with v-model bound to favoriteFruit.
Add a paragraph below the radio buttons that shows the selected fruit dynamically.
💡 Why This Matters
🌍 Real World
Forms often require users to select one option from many, such as choosing preferences or survey answers. Binding radio buttons with v-model makes this easy and reactive.
💼 Career
Understanding how to bind form inputs in Vue is essential for frontend developers building interactive user interfaces.
Progress0 / 4 steps
1
Setup the Vue component and data property
Create a Vue component with a reactive data property called favoriteFruit initialized to an empty string ''.
Vue
Hint
Use ref from Vue to create a reactive variable.
2
Add the fruits array
Add a constant array called fruits with these exact values: 'Apple', 'Banana', 'Cherry'.
Vue
Hint
Use const fruits = ['Apple', 'Banana', 'Cherry'] to create the array.
3
Render radio buttons with v-model
Inside the <div>, use v-for to create a radio button for each fruit in fruits. Bind each radio button's v-model to favoriteFruit. Set the value attribute of each radio button to the fruit name.
Vue
Hint
Use v-for="fruit in fruits" on a <label> and bind v-model on the <input type="radio">.
4
Show the selected fruit
Below the radio buttons, add a paragraph that displays the text Your favorite fruit is: followed by the reactive variable favoriteFruit using Vue's interpolation syntax.
Vue
Hint
Use {{ favoriteFruit }} inside a paragraph to show the selected fruit.
Practice
(1/5)
1. What does v-model do when used with radio buttons in Vue?
easy
A. It binds the selected radio button's value to a single data property.
B. It creates multiple independent data properties for each radio button.
C. It disables all radio buttons except the first one.
D. It automatically submits the form when a radio button is clicked.
Solution
Step 1: Understand v-model binding
The v-model directive 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-model binds the selected button's value to 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 A
Quick Check:
v-model syncs selected radio value = C [OK]
Hint: v-model links one data value to all radios in a group [OK]
Common Mistakes:
Thinking each radio has its own data property
Assuming v-model disables radios
Believing v-model auto-submits forms
2. Which of the following is the correct syntax to bind a radio button group with v-model to a data property named choice?
easy
A. <input type='radio' :model='choice' value='A'>
B. <input type='radio' v-bind='choice' value='A'>
C. <input type='radio' v-model='value' value='choice'>
D. <input type='radio' v-model='choice' value='A'>
Solution
Step 1: Identify correct directive usage
The correct directive to bind input value to data is v-model, not v-bind or :model.
Step 2: Check attribute values
The value attribute sets the radio's value, and v-model='choice' binds the selected value to the choice data property.
Final Answer:
<input type='radio' v-model='choice' value='A'> -> Option D
Quick Check:
Use v-model='choice' with value='A' = A [OK]
Hint: Use v-model='dataProp' and value='option' for radios [OK]
Why might none of the radio buttons appear selected even after clicking them?
medium
A. Radio buttons require a name attribute to work with v-model.
B. The data property selection is not initialized in the Vue component.
C. The v-model directive is misspelled.
D. The value attributes are missing from the radio buttons.
Solution
Step 1: Check data initialization
Vue's v-model binds to a data property. If selection is undefined initially, no radio is selected by default.
Step 2: Understand radio selection behavior
Clicking a radio sets selection to 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 property selection is not initialized in the Vue component. -> Option B
Quick Check:
Uninitialized data property causes no selection [OK]
Hint: Always initialize data properties used with v-model [OK]
Common Mistakes:
Thinking name attribute is required for v-model radios
Ignoring data property initialization
Assuming misspelled v-model without checking
5. You want to create a group of radio buttons bound to 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?