Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Forms Handling  ›  Lesson

Checkboxes, Radios and Selects

v-model 14 minv-model adapts to each input type: a checkbox gives a boolean or an array, a radio gives its value, a select gives the chosen option's value.

Checkboxes, Radios and Selects

The Add Book form grows: a "Finished already" tick box, genre tick boxes, a choice of format, and a dropdown for the shelf it goes on. All of them use v-model, but each writes a different kind of value. Get the ref's starting type right and the rest follows.

The rule

The input's type and the ref's type together decide what v-model writes.

<script setup>
import { ref } from 'vue'

const finished = ref(false)        // one checkbox → boolean
const genres = ref([])             // many checkboxes → array
const format = ref('paper')        // radios → one value
const shelf = ref('')              // select → one value
</script>

<template>
  <label><input type="checkbox" v-model="finished" /> Finished already</label>

  <label><input type="checkbox" value="fiction" v-model="genres" /> Fiction</label>
  <label><input type="checkbox" value="science" v-model="genres" /> Science</label>
  <label><input type="checkbox" value="history" v-model="genres" /> History</label>

  <label><input type="radio" value="paper" v-model="format" /> Paper</label>
  <label><input type="radio" value="ebook" v-model="format" /> Ebook</label>
  <label><input type="radio" value="audio" v-model="format" /> Audio</label>

  <select v-model="shelf">
    <option disabled value="">Choose a shelf</option>
    <option value="now">Reading now</option>
    <option value="later">Read later</option>
  </select>
</template>

Type by type:

  • One checkbox, boolean ref. Ticked → true, unticked → false. If you want other values, add true-value="yes" and false-value="no".
  • Several checkboxes, one array ref. Each has its own value. Ticking pushes that value into the array; unticking removes it. Tick Fiction then History and genres is ['fiction', 'history'].
  • Radios, one shared ref. The ref holds the value of whichever radio is selected. Because all three share format, only one can be chosen. Setting ref('paper') pre-selects Paper.
  • Select, one ref. The ref holds the chosen option's value. The disabled empty option acts as a prompt while shelf is ''. Add the multiple attribute and bind an array to allow several choices.

Values do not have to be strings: :value="3" or :value="{ id: 3 }" binds real numbers or objects.

The shortcut

Before writing the field, ask: "One yes or no, one choice from many, or several choices?"

  • One yes or no → single checkbox, boolean.
  • One choice from many → radios or a select, string (or number).
  • Several choices → checkboxes or select multiple, array.
Designing the format fieldStep 1 of 4
  1. Ask the question

    A book is in one format at a time. One choice from many.

Drills

Answer fast — trust the rule.

quick drill

A single checkbox is bound to ref(false). What does ticking it store?

quick drill

Three genre checkboxes share v-model="genres" with genres = ref([]). The user ticks Science. What is genres.value?

quick drill

Three radios share v-model="format". Paper is selected. What does format.value hold?

quick drill

Which question-and-control pair is correct?

Remember

  • Single checkbox → boolean; group of checkboxes → array; radios and select → one value.
  • Shortcut: "one yes or no, one choice, or several?" picks both the control and the ref type.
  • Trap: binding a checkbox group to a boolean or a string instead of an array.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainThe add-book form

The Add Book form has a 'Finished' checkbox, a set of genre checkboxes (fiction, science, history) and a format picker (paper, ebook, audio). Explain what type of ref each needs and why.