Vue.js Form Input Value Binding (original) (raw)

Last Updated : 06 Apr, 2022

Vue.js is a progressive javascript framework for developing web user interfaces. It is a performant, approachable, and versatile framework. We can create Single Page Applications as well as Full Stack applications. It is built on top of HTML, CSS, and Javascript which makes it easier for developers to integrate Vue.js in any application at any stage.

Form Input Value Binding is handled by the v-model directive that helps to bind the static strings, to simplify the input value binding. The v-model can be used on different types of elements like input, textarea, checkbox, radio, and select. It automatically adjusts to the different DOM properties and event pairs as per the elements used.

The _v-model_can be used to create two-way data bindings on form input and textarea. The input with text types and textarea uses value property and input event to handle value binding. It does not count the placeholder text and initial value for checkbox, radio, and select elements.

Form Input Value Binding v-model for different elements:

Syntax: For the different input elements, we have different syntax and hence different approaches. Here is the list of syntaxes for the different elements.

{{ message }}

Installation Process:

Step 1: Create a new Vue.js project with the npm node.js package manager using the following command.

npm init vue@latest

Enter the project name and preset the project as follows:

Project Structure: After successful installation, the following project structure will be formed.

Project Structure

Example 1: input element - Single-line input

Here we have a single line input element with v-model as the name variable. The v-model will save the updated value in the input element.

App.vue `

GeeksforGeeks

Vue.js Form Input Value Binding

Input Element

Name: {{ name }}

`

Output:

Example 2: textarea element - multiline input

In this example, we have a textarea element that can store multiline input, and the v-model will handle the change event and store the updated value in a variable called about.

App.vue `

GeeksforGeeks

Vue.js Form Input Value Binding

Textarea Element

{{ about }}

`

Output:

Example 3: checkbox element

In this example, we have a checkbox type of input element where we can choose either true or false based on our selection of the input element.

App.vue `

GeeksforGeeks

Vue.js Form Input Value Binding

Checkbox Element

GeeksforGeeks is best computer science portal for geeks: {{ check }}

`

Output:

Example 4: radio element

In the following example, we have three radio options from which we can choose any one of them and the chosen value will be stored by the v-model which we can display on our webpage.

App.vue `

GeeksforGeeks

Vue.js Form Input Value Binding

Radio Input Element

Tutorial: {{ tutorial }}

Data Structures Machine Learning Web Development

`

Output:

Example 5: select element

In this example, we have a list of items in the select element from where we will select one of the options. The v-model is written for only the select element and fetches the value from the option elements by itself.

App.vue `

GeeksforGeeks

Vue.js Form Input Value Binding

Select Element

Tutorial: {{ tutorial }}

Choose tutorial

`

Output:

Reference: https://vuejs.org/guide/essentials/forms.html#value-bindings