📖 Scenario: You are building a simple form in Vue where users can choose their favorite fruit from a dropdown list.
🎯 Goal: Create a Vue component that uses v-model to bind the selected fruit from a dropdown menu to a data variable.
📋 What You'll Learn
Create a Vue component with a data variable called selectedFruit initialized to an empty string.
Create a list of fruits as an array called fruits with these exact values: 'Apple', 'Banana', 'Cherry', 'Date'.
Use a <select> element with v-model bound to selectedFruit.
Use <option> elements generated by looping over fruits to show each fruit as a choice.
Display the currently selected fruit below the dropdown.
💡 Why This Matters
🌍 Real World
Dropdown menus are common in forms for selecting options like countries, categories, or preferences. Binding them with v-model keeps the UI and data in sync easily.
💼 Career
Understanding v-model with select elements is essential for building interactive Vue applications and forms, a common task in frontend development jobs.
Progress0 / 4 steps
1
Set up the fruits array
Create a Vue component with a fruits array containing these exact strings: 'Apple', 'Banana', 'Cherry', 'Date'.
Vue
Hint
Use const fruits = ['Apple', 'Banana', 'Cherry', 'Date'] inside the <script setup> block.
2
Add selectedFruit data variable
Inside the <script setup>, create a ref called selectedFruit and initialize it to an empty string ''.
Vue
Hint
Import ref from 'vue' and create const selectedFruit = ref('').
3
Bind select dropdown with v-model
In the <template>, add a <select> element with v-model="selectedFruit". Inside it, use v-for to create <option> elements for each fruit in fruits. Use :key with the fruit name and set the option's value to the fruit.
Vue
Hint
Use <select v-model="selectedFruit"> and inside it <option v-for="fruit in fruits" :key="fruit" :value="fruit">{{ fruit }}</option>.
4
Display the selected fruit
Below the <select>, add a <p> element that shows the text "Selected fruit: " followed by the value of selectedFruit using mustache syntax.
Vue
Hint
Use <p>Selected fruit: {{ selectedFruit }}</p> below the select.
Practice
(1/5)
1. What does v-model do when used on a <select> dropdown in Vue?
easy
A. It binds the selected option's value to a Vue data property.
B. It disables the dropdown from user interaction.
C. It automatically submits the form when an option is selected.
D. It changes the dropdown options dynamically without user input.
Solution
Step 1: Understand v-model binding
v-model creates a two-way binding between the dropdown's selected value and a Vue data property.
Step 2: Effect on dropdown selection
When a user picks an option, the bound data property updates to that option's value automatically.
Final Answer:
It binds the selected option's value to a Vue data property. -> Option A
Quick Check:
v-model binds selection to data [OK]
Hint: v-model syncs dropdown choice with data property [OK]
Common Mistakes:
Thinking v-model disables the dropdown
Assuming v-model submits forms automatically
Believing v-model changes options without user action
2. Which of the following is the correct syntax to bind a v-model to a <select> element in Vue?
easy
A.
B.
C.
D.
Solution
Step 1: Recall Vue directive syntax
v-model is used as a directive without colon or parentheses on form elements.
Assuming selected is initially "two", what issue will occur?
medium
A. The selected option will always be the first option regardless of v-model.
B. The dropdown will not render because of missing value on first option.
C. The v-model will throw a syntax error due to missing value attribute.
D. The first option has no value, so it binds as 'One' by default causing mismatch.
Solution
Step 1: Check option values
The first option lacks a value attribute, so its value defaults to its text content "One".
Step 2: Compare initial v-model value
Since selected is "two", Vue tries to match it with option values. It matches the second option, but the first option's missing value can cause confusion or unexpected behavior.
Final Answer:
The first option has no value, so it binds as 'One' by default causing mismatch. -> Option D
Quick Check:
Options without value default to text, causing binding issues [OK]
Hint: Always add value attribute to all options [OK]
Common Mistakes:
Assuming missing value means empty string
Expecting Vue to auto-assign numeric values
Ignoring mismatch between v-model and option values
5. You want to create a dropdown with multiple selections using v-model in Vue. Which is the correct way to bind the <select> element to an array of selected values?
hard
A.
B.
C.
D.
Solution
Step 1: Understand multiple attribute usage
To allow multiple selections, the multiple attribute is added without a value or with boolean true.
Step 2: Check v-model binding syntax
Binding directly to an array property like selectedFruits works. Adding brackets or setting multiple as a string is incorrect.
Final Answer:
<select v-model="selectedFruits" multiple> -> Option C
Quick Check:
Use multiple attribute and bind v-model to array [OK]
Hint: Use multiple attribute and bind v-model to array property [OK]
Common Mistakes:
Adding brackets [] in v-model binding
Setting multiple="true" as string attribute
Using :multiple="true" unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.