🎓 A1Learner
🔍
0 of 50 lessons completed

Vue Checkboxes and Selects

A checkbox is a small box that a person ticks or unticks. A select is a drop-down list with one choice picked from a list of options.

One checkbox

This box links to a ref that starts as false.

<input type="checkbox" v-model="agree">

A select

The select links to a ref that starts as Apple.

<select v-model="fruit">
  <option>Apple</option>
  <option>Banana</option>
</select>

Checkboxes share an array

Each ticked box adds its value to the shared array.

<input type="checkbox" value="Apple" v-model="picks">

Example

<script setup>
import { ref } from 'vue'
const agree = ref(false)
const fruit = ref('Apple')
</script>
<template>
  <div>
    <label><input type="checkbox" v-model="agree"> News</label>
    <select v-model="fruit">
      <option>Apple</option>
      <option>Banana</option>
      <option>Cherry</option>
    </select>
    <p>News: {{ agree }}</p>
    <p>Fruit: {{ fruit }}</p>
  </div>
</template>

Key points

  • A checkbox holds a yes-or-no answer, so it links to a ref that starts as true or false.
  • A select shows a list of options, and the person picks one.
  • An option is one choice inside a select.
  • v-model on a select links the picked option to a ref.
  • A group of checkboxes can share one array ref, and each ticked box adds its value to the array.

In this example, agree is a ref that starts as false. fruit is a ref that starts as Apple. The checkbox links to agree, and the select links to fruit. Two paragraphs show the two answers. The page starts with an unticked box and Apple as the pick. Ticking the box and picking Banana changes the answers to true and Banana.

Note

A ticked box adds its value, and an unticked box adds nothing.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

What does v-model link on a select?