Menu
CoddyTech

Input Modifiers

Part of the Fundamentals section of Coddy's Vue journey. Lesson 19 of 55.

Modifiers adjust how a form value reaches state. With the example below, typing spaces around Plan stores Plan; typing 6 into the amount field stores a number, so adding one yields 7.

const title = ref('');
const amount = ref(0);
// <input v-model.trim="title">
// <input v-model.number="amount">
// <p>{{ amount + 1 }}</p>

.trim removes whitespace at the ends, not within the text. .number parses numeric input where possible; an empty or unparseable value is not guaranteed to become a number. Handle those cases when your app needs validation. Here, use valid numeric values.

challenge icon

Challenge

Easy

Use v-model.trim on #label and v-model.number on #hours. Start with an empty label and zero hours. In #result, show the trimmed label, a colon and space, and the numeric hours plus one. Tests use valid numeric input.

Try it yourself

<!doctype html><html><head><meta charset="UTF-8"><title>Vue practice</title></head><body><div id="app"></div><script type="module" src="./main.js"></script></body></html>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals