Checkbox Models
Part of the Fundamentals section of Coddy's Vue journey. Lesson 17 of 55.
For a single checkbox, v-model reads and writes a boolean. This setting starts checked because the source ref is true. Unchecking changes the paragraph to false.
const alerts = ref(true);
// <input type="checkbox" v-model="alerts">
// <p>{{ alerts }}</p>The checked state and the value attribute have different meanings. Use a boolean ref for an independent on/off setting, and use a separate ref when another checkbox must be independent. Labels make the setting understandable and clickable.
Challenge
EasyBind input#consent, a checkbox, to a boolean ref starting false. Display its boolean value in #result. Both checking and unchecking must update state.
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>This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
2Reactive State
Reactive RefsUpdating Refs in ScriptEvent Handler ArgumentsReactive ObjectsRecap: Reactive State5Conditions and Lists
Conditional BranchesShow without UnmountingRendering ListsStable List IdentityRecap: Conditions8Component Communication
Emitting EventsEvent PayloadsLifting Shared StateDefault and Named SlotsRecap: Shared Panel11Project: Community Board
Board StructureAdding Community Tasks