v-model helps you easily connect a dropdown menu to your data. It keeps the selected option and your data in sync without extra code.
v-model with select dropdowns in Vue
<select v-model="selectedOption"> <option value="option1">Option 1</option> <option value="option2">Option 2</option> <option value="option3">Option 3</option> </select>
The v-model directive binds the dropdown's selected value to a data property.
Make sure the value attributes on option tags match the data type you expect.
fruit data property.<select v-model="fruit"> <option value="apple">Apple</option> <option value="banana">Banana</option> <option value="cherry">Cherry</option> </select>
<select v-model="color"> <option disabled value="">Please select a color</option> <option value="red">Red</option> <option value="green">Green</option> <option value="blue">Blue</option> </select>
v-for. The selected number is bound to selectedNumber.<select v-model="selectedNumber"> <option v-for="num in [1,2,3]" :key="num" :value="num">{{ num }}</option> </select>
This Vue component shows a dropdown to pick a car brand. The selected car updates the selectedCar variable automatically. The chosen car is displayed below the dropdown. The label and aria-label improve accessibility.
<template>
<div>
<label for="car-select">Choose a car:</label>
<select id="car-select" v-model="selectedCar" aria-label="Car selection">
<option disabled value="">Select a car</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
<p>You selected: {{ selectedCar || 'none' }}</p>
</div>
</template>
<script setup>
import { ref } from 'vue'
const selectedCar = ref('')
</script>Use v-model on <select> to keep the selected value and data in sync automatically.
Always provide a label for accessibility and use aria-label if the label is not visible.
When using numbers or booleans as values, remember that v-model binds the value as a string by default. Use :value binding to keep the original type.
v-model connects dropdown selection to your data easily.
Use value attributes on options to define what each choice means.
Labels and ARIA attributes help everyone use your dropdown comfortably.
Practice
v-model do when used on a <select> dropdown in Vue?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 AQuick Check:
v-model binds selection to data [OK]
- Thinking v-model disables the dropdown
- Assuming v-model submits forms automatically
- Believing v-model changes options without user action
v-model to a <select> element in Vue?Solution
Step 1: Recall Vue directive syntax
v-model is used as a directive without colon or parentheses on form elements.Step 2: Check each option's syntax
uses correct syntax:v-model="selectedOption". Others misuse binding or add invalid characters.Final Answer:
<select v-model="selectedOption"> -> Option AQuick Check:
Use v-model="property" without colon or parentheses [OK]
- Adding colon before v-model
- Using parentheses after data property
- Using v-bind:model which is invalid
<select v-model="fruit">
<option value="apple">Apple</option>
<option value="banana">Banana</option>
<option value="cherry">Cherry</option>
</select>
<p>Selected: {{ fruit }}</p>If the user selects "Banana", what will be displayed inside the <p> tag?
Solution
Step 1: Understand option values vs labels
The option'svalueattribute is "banana" (lowercase), label is "Banana" (capitalized).Step 2: v-model binds to option value
When user selects "Banana", the bound data propertyfruitbecomes "banana" (the value attribute).Final Answer:
Selected: banana -> Option BQuick Check:
v-model binds to option value, not label [OK]
- Assuming v-model binds to option label text
- Expecting data property to show variable name
- Confusing case sensitivity in values
<select v-model="selected"> <option>One</option> <option value="two">Two</option> <option value="three">Three</option> </select>
Assuming
selected is initially "two", what issue will occur?Solution
Step 1: Check option values
The first option lacks avalueattribute, so its value defaults to its text content "One".Step 2: Compare initial v-model value
Sinceselectedis "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 DQuick Check:
Options without value default to text, causing binding issues [OK]
- Assuming missing value means empty string
- Expecting Vue to auto-assign numeric values
- Ignoring mismatch between v-model and option values
v-model in Vue. Which is the correct way to bind the <select> element to an array of selected values?Solution
Step 1: Understand multiple attribute usage
To allow multiple selections, themultipleattribute is added without a value or with boolean true.Step 2: Check v-model binding syntax
Binding directly to an array property likeselectedFruitsworks. Adding brackets or setting multiple as a string is incorrect.Final Answer:
<select v-model="selectedFruits" multiple> -> Option CQuick Check:
Use multiple attribute and bind v-model to array [OK]
- Adding brackets [] in v-model binding
- Setting multiple="true" as string attribute
- Using :multiple="true" unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
