← Back to Vue.js Course | Chapter 5: Forms | Lesson 1 of 6

v-model basics

v-model links a form input to a piece of data in both directions.

In this page:

  1. v-model basics
Syntax
markup
<input v-model="property">
<p>{{ property }}</p>

v-model basics

v-model is sugar for binding the value and listening to input events. Typing updates the data and changing the data updates the input. It works on text inputs, textareas, checkboxes, radios and selects, choosing the right property and event for each.

Note: v-model ignores the initial value attribute; set the initial value in data.

Example: v-model basics

markup
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
</head>
<body>
<div id="app">
  <input v-model="name" placeholder="Your name">
  <p>Hello, {{ name || "stranger" }}!</p>
</div>
<script>
  Vue.createApp({ data() { return { name: "" }; } }).mount("#app");
  const input = document.querySelector("input");
  input.value = "Ada";
  input.dispatchEvent(new window.Event("input"));
  Vue.nextTick(() => console.log(document.querySelector("p").textContent));
</script>
</body>
</html>

<!-- Output:
Rendered: Hello, Ada!
console: Hello, Ada!
-->
Live Example
Related Topics
Common Mistakes
  1. Using an initial value attribute
  2. Expecting v-model to work on non-form elements
  3. Forgetting to declare the data property
Chapter Summary
  • v-model is two-way binding
  • Works on inputs, textareas, selects
  • Data holds the initial value
  • Sugar for :value plus @input
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.