Form Input Bindings

Basic Usage

You can use the v-model directive to create two-way data bindings on form input, textarea, and select elements. It automatically picks the correct way to update the element based on the input type. Although a bit magical, v-model is essentially syntax sugar for updating data on user input events, plus special care for some edge cases.

Note

v-model will ignore the initial value, checked or selected attributes found on any form elements. It will always treat the Vue instance data as the source of truth. You should declare the initial value on the JavaScript side, inside the data option of your component.

v-model internally uses different properties and emits different events for different input elements:

  • text and textarea elements use value property and input event;
  • checkboxes and radiobuttons use checked property and change event;
  • select fields use value as a prop and change as an event.

Note

For languages that require an IMEForm Input Bindings - 图1 (Chinese, Japanese, Korean etc.), you’ll notice that v-model doesn’t get updated during IME composition. If you want to cater for these updates as well, use input event instead.

Text

  1. <input v-model="message" placeholder="edit me" />
  2. <p>Message is: {{ message }}</p>

See the Pen Handling forms: basic v-model by Vue (@Vue) on CodePen.

Multiline text

  1. <span>Multiline message is:</span>
  2. <p style="white-space: pre-line;">{{ message }}</p>
  3. <br />
  4. <textarea v-model="message" placeholder="add multiple lines"></textarea>

See the Pen Handling forms: textarea by Vue (@Vue) on CodePen.

Interpolation on textareas won’t work. Use v-model instead.

  1. <!-- bad -->
  2. <textarea>{{ text }}</textarea>
  3. <!-- good -->
  4. <textarea v-model="text"></textarea>

Checkbox

Single checkbox, boolean value:

  1. <input type="checkbox" id="checkbox" v-model="checked" />
  2. <label for="checkbox">{{ checked }}</label>

See the Pen Handling forms: checkbox by Vue (@Vue) on CodePen.

Multiple checkboxes, bound to the same Array:

  1. <div id="v-model-multiple-checkboxes">
  2. <input type="checkbox" id="jack" value="Jack" v-model="checkedNames" />
  3. <label for="jack">Jack</label>
  4. <input type="checkbox" id="john" value="John" v-model="checkedNames" />
  5. <label for="john">John</label>
  6. <input type="checkbox" id="mike" value="Mike" v-model="checkedNames" />
  7. <label for="mike">Mike</label>
  8. <br />
  9. <span>Checked names: {{ checkedNames }}</span>
  10. </div>
  1. Vue.createApp({
  2. data() {
  3. return {
  4. checkedNames: []
  5. }
  6. }
  7. }).mount('#v-model-multiple-checkboxes')

See the Pen Handling forms: multiple checkboxes by Vue (@Vue) on CodePen.

Radio

  1. <div id="v-model-radiobutton">
  2. <input type="radio" id="one" value="One" v-model="picked" />
  3. <label for="one">One</label>
  4. <br />
  5. <input type="radio" id="two" value="Two" v-model="picked" />
  6. <label for="two">Two</label>
  7. <br />
  8. <span>Picked: {{ picked }}</span>
  9. </div>
  1. Vue.createApp({
  2. data() {
  3. return {
  4. picked: ''
  5. }
  6. }
  7. }).mount('#v-model-radiobutton')

See the Pen Handling forms: radiobutton by Vue (@Vue) on CodePen.

Select

Single select:

  1. <div id="v-model-select" class="demo">
  2. <select v-model="selected">
  3. <option disabled value="">Please select one</option>
  4. <option>A</option>
  5. <option>B</option>
  6. <option>C</option>
  7. </select>
  8. <span>Selected: {{ selected }}</span>
  9. </div>
  1. Vue.createApp({
  2. data() {
  3. return {
  4. selected: ''
  5. }
  6. }
  7. }).mount('#v-model-select')

See the Pen Handling forms: select by Vue (@Vue) on CodePen.

Note

If the initial value of your v-model expression does not match any of the options, the <select> element will render in an “unselected” state. On iOS this will cause the user not being able to select the first item because iOS does not fire a change event in this case. It is therefore recommended to provide a disabled option with an empty value, as demonstrated in the example above.

Multiple select (bound to Array):

  1. <select v-model="selected" multiple>
  2. <option>A</option>
  3. <option>B</option>
  4. <option>C</option>
  5. </select>
  6. <br />
  7. <span>Selected: {{ selected }}</span>

See the Pen Handling forms: select bound to array by Vue (@Vue) on CodePen.

Dynamic options rendered with v-for:

  1. <div id="v-model-select-dynamic" class="demo">
  2. <select v-model="selected">
  3. <option v-for="option in options" :value="option.value">
  4. {{ option.text }}
  5. </option>
  6. </select>
  7. <span>Selected: {{ selected }}</span>
  8. </div>
  1. Vue.createApp({
  2. data() {
  3. return {
  4. selected: 'A',
  5. options: [
  6. { text: 'One', value: 'A' },
  7. { text: 'Two', value: 'B' },
  8. { text: 'Three', value: 'C' }
  9. ]
  10. }
  11. }
  12. }).mount('#v-model-select-dynamic')

See the Pen Handling forms: select with dynamic options by Vue (@Vue) on CodePen.

Value Bindings

For radio, checkbox and select options, the v-model binding values are usually static strings (or booleans for checkbox):

  1. <!-- `picked` is a string "a" when checked -->
  2. <input type="radio" v-model="picked" value="a" />
  3. <!-- `toggle` is either true or false -->
  4. <input type="checkbox" v-model="toggle" />
  5. <!-- `selected` is a string "abc" when the first option is selected -->
  6. <select v-model="selected">
  7. <option value="abc">ABC</option>
  8. </select>

But sometimes we may want to bind the value to a dynamic property on the Vue instance. We can use v-bind to achieve that. In addition, using v-bind allows us to bind the input value to non-string values.

Checkbox

  1. <input type="checkbox" v-model="toggle" true-value="yes" false-value="no" />
  1. // when checked:
  2. vm.toggle === 'yes'
  3. // when unchecked:
  4. vm.toggle === 'no'

Tip

The true-value and false-value attributes don’t affect the input’s value attribute, because browsers don’t include unchecked boxes in form submissions. To guarantee that one of two values is submitted in a form (e.g. “yes” or “no”), use radio inputs instead.

Radio

  1. <input type="radio" v-model="pick" v-bind:value="a" />
  1. // when checked:
  2. vm.pick === vm.a

Select Options

  1. <select v-model="selected">
  2. <!-- inline object literal -->
  3. <option :value="{ number: 123 }">123</option>
  4. </select>
  1. // when selected:
  2. typeof vm.selected // => 'object'
  3. vm.selected.number // => 123

Modifiers

.lazy

By default, v-model syncs the input with the data after each input event (with the exception of IME composition as stated above). You can add the lazy modifier to instead sync after change events:

  1. <!-- synced after "change" instead of "input" -->
  2. <input v-model.lazy="msg" />

.number

If you want user input to be automatically typecast as a number, you can add the number modifier to your v-model managed inputs:

  1. <input v-model.number="age" type="number" />

This is often useful, because even with type="number", the value of HTML input elements always returns a string. If the value cannot be parsed with parseFloat(), then the original value is returned.

.trim

If you want whitespace from user input to be trimmed automatically, you can add the trim modifier to your v-model-managed inputs:

  1. <input v-model.trim="msg" />

v-model with Components

If you’re not yet familiar with Vue’s components, you can skip this for now.

HTML’s built-in input types won’t always meet your needs. Fortunately, Vue components allow you to build reusable inputs with completely customized behavior. These inputs even work with v-model! To learn more, read about custom inputs in the Components guide.