Vue3 Fundamentals ยท Events & Forms

Binding checkboxes, radios, and selects

v-model across input types

Binding checkboxes, radios, and selects

Text inputs are not the only fields a form has. A checkbox answers a yes-or-no question, a radio group picks one of several, and a select picks one from a dropdown. Each stores its value differently, so binding them by hand means remembering which event and which property to read.

v-model handles all three, and it adapts to the input's type.

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

const agreed = ref(false);
const size = ref('medium');
</script>

<template>
  <input type="checkbox" v-model="agreed" />
  <input type="radio" value="small" v-model="size" />
  <input type="radio" value="large" v-model="size" />
  <select v-model="size">
    <option value="small">Small</option>
    <option value="large">Large</option>
  </select>
</template>

On a checkbox, v-model binds a boolean: checked means true. On a radio or a select, it binds the value of the chosen option, so a radio group shares one ref and the selected one writes its value into it. Give several checkboxes the same array ref and v-model collects the checked values into the array.

Your job is to bind a checkbox to a boolean in App.vue.

Challenge

The checkbox on the page does not change the message below it. Export an App component from App.vue that renders a checkbox bound with v-model to a boolean ref, and a <p> that reads Agreed when the ref is true and Not yet when it is false.

The Test checks the box and expects the paragraph to read Agreed. The starter renders the checkbox with no binding, so the ref stays false and the Test fails.

App.vue
<script setup>
import { ref } from 'vue';

const agreed = ref(false);
</script>

<template>
  <input type="checkbox" />
  <p>{{ agreed ? 'Agreed' : 'Not yet' }}</p>
</template>

No test run yet

The Test is hidden. Run it to check the Challenge โ€” you only see the result, never the test source.