How to set checked radio button in VueJS?

Hi Friends 👋,

Welcome To SortoutCode! ❤️

To set the checked radio button in VueJS. We are going to use the v-model component of VueJS. Using the component v-model we can implement the two-way binding. That means every time user changes the value of the input field, it automatically updated the text variable.

Short solution

FirstComp.vue

<template>
<label><input type="radio" value="true" v-model="rdata"/>True</label>
<label><input type="radio" value="false" v-model="rdata"/>False</label>
</template>
<script>
export default {
  name: "FirstComp",
  data() {
    return {
      rdata:'true',
    };
  },
};
</script>

Today, I am going to show you How you set the checked radio button in VueJS?

Table of Content

  • Setup the Vue (Optional)
  • Create FirstComponent.vue and import it into App.js
  • How to show the checked radio button?

This article will guide you to How do I show checked radio button in vueJS

Setup the Vue.js

First, we have to install the Vue project, I had installed the vueJS in my system. If you haven’t installed or have any problem with installation you can follow this article, it will show you step by step process of installation.

How to Install the VueJS project?

Create FirstComponent.vue and import it into App.js

Create the new component in the src/components/ components folder. The component name is FirstComponent.vue and imported into the App.js file:

App.js

<template>
  <FirstComp />
</template>

<script>
import FirstComp from "./components/FirstComp.vue";
export default {
  name: "App",
  components: {
    FirstComp: FirstComp,
  },
};
</script>

How to show the checked radio button?

To display the radio button checked in vuejs. We are going to use the v-model component which is provided by the vuejs. Component v-model is used as two-way data binding.

Let’s take a short example to better understand.

FirstComp.vue

<template>
  <div class="app-data">
    <h1>Welcome to sortoutcode.com</h1>
    <label
      ><input
        type="radio"
        value="Sortoutcode"
        v-model="website"
      />Sortoutcode</label
    >
    <label
      ><input
        type="radio"
        value="aguidehub"
        v-model="website"
      />Aguidehub</label
    >
    <label
      ><input
        type="radio"
        value="infinitbility"
        v-model="website"
      />infinitbility</label
    >
    <p v-if="getchecked">{{ getchecked }}</p>
    <button @click="showData">Show Data</button>
  </div>
</template>
<script>
export default {
  name: "FirstComp",
  data() {
    return {
      website: "aguidehub",
      getchecked: "",
    };
  },
  methods: {
    showData() {
      this.getchecked = this.website;
    },
  },
};
</script>

For now, let’s check the output.

Output

Show selected radio button

Links

Here, is the above program code sandbox link of how to set the checked radio button in vue js. Then you can use it whenever you went and do the changes as per your requirements.

Codesandbox

All the best 👍.

Follow me on Twitter