vuejs set a radio button checked if statement is true

vue.js

vue.js Problem Overview


I am trying to make a radio button checked using vuejs v-for only if my if-statement is true. Is there a way to use vuejs' v-if/v-else for this type of problem?

in php and html I can achieve this by doing the following:

<input type="radio" <? if(portal.id == currentPortalId) ? 'checked="checked"' : ''?>>

Below is what I have so far using vuejs:

    <div v-for="portal in portals">
     <input type="radio" id="{{portal.id}}" name="portalSelect"
       v-bind:value="{id: portal.id, name: portal.name}"
       v-model="newPortalSelect"
       v-on:change="showSellers"
       v-if="{{portal.id == currentPortalId}}"
       checked="checked">
     <label for="{{portal.id}}">{{portal.name}}</label>
    </div>

I know the v-if statement here is for checking whether to show or hide the input.

Any help would be very much appreciated.

vue.js Solutions


Solution 1 - vue.js

You could bind the checked attribute like this:

<div v-for="portal in portals">
  <input type="radio"
         id="{{portal.id}}"
         name="portalSelect"
         v-bind:value="{id: portal.id, name: portal.name}"
         v-model="newPortalSelect"
         v-on:change="showSellers"
         :checked="portal.id == currentPortalId">

  <label for="{{portal.id}}">{{portal.name}}</label>
</div>

Simple example: https://jsfiddle.net/b4k6tpj9/

Solution 2 - vue.js

Maybe someone finds this approach helpful:

In template I assign each radio button a value:

<input type="radio" value="1" v-model.number="someProperty">
<input type="radio" value="2" v-model.number="someProperty">

Then in the component I set the value, i.e:

data: function () {
    return {
        someProperty: 2
    }
}

And in this case vue will select the second radio button.

Solution 3 - vue.js

You can follow below option if you can adjust with your logic:

<div class="combination-quantity">
    <input type="radio" value="Lost" 
    v-model="missing_status">
    <label for="lost">Lost</label>
    <br>
    <input type="radio" value="Return Supplier" v-model="missing_status">
    <label for="return_supplier">Return Supplier</label>
</div>

Value for missing_status could be "Lost" or "Return Supplier" and based on the value radio option will be get selected automatically.

Solution 4 - vue.js

Below is an example of keeping track of the selected radiobutton, by

  • applying a value binding to the object (:value="portal") and
  • applying a v-model binding to the currently selected object (v-model="currentPortal").

The radiobutton will be checked automatically by Vue, when the two match (no :checked binding necessary!).

Vue 3 with composition API

Vue.createApp({
  setup() {
    const portals = [{
      id: 1,
      name: "Portal 1"
    }, {
      id: 2,
      name: "Portal 2"
    }];
    
    const currentPortal = portals[1];
    
    return {
      portals,
      currentPortal
    }
  }
}).mount("#app");

<script src="https://unpkg.com/vue@next"></script>

<div id="app">
  <template v-for="portal in portals">
    <input 
      type="radio" 
      :id="portal.id"
      name="portalSelect" 
      :value="portal" 
      v-model="currentPortal">

    <label :for="portal.id">{{portal.name}}</label>
  </template>
</div>

Solution 5 - vue.js

I would like to point out a few options when dealing with radios and vue.js. In general if you need to dynamically bind an attribute value you can use the shorthand binding syntax to bind to and calculate that value. You can bind to data, a computed value or a method and a combination of all three.

new Vue({
  el: '#demo',
  data() {
    return {
      checkedData: false,
      checkedGroupVModel: "radioVModel3", //some defaul
      toggleChecked: false,
      recalculateComputed: null
    };
  },
  computed: {
    amIChecked() {
      let isEven = false;
      if (this.recalculateComputed) {
        let timeMills = new Date().getMilliseconds();
        isEven = timeMills % 2 === 0;
      }
      return isEven;
    }
  },
  methods: {
    onToggle() {
      this.toggleChecked = !this.toggleChecked;
      return this.toggleChecked;
    },
    mutateComputedDependentData() {
      this.recalculateComputed = {};
    }
  }
})

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.min.js"></script>
<div id="demo">
  <div>
    <div>
      <span>Simple Radio Group - Only one checked at a time. Bound to data.checkedData</span><br>
      <label>Radio 1 - inverse of checkedData = {{!checkedData}}
        <input type="radio" name="group1" value="radio1" :checked="!checkedData">
      </label><br>
      <label>Radio 2 - checkedData = {{checkedData}}
        <input type="radio" name="group1" value="radio2" :checked="checkedData">
      </label><br>
      <span>Understanding checked attribute: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-checked</span>
    </div>
    <br>
    <div>
      <span>Simple Radio - Checked bouned to semi-random computed object</span><br>
      <label>Radio 1: {{amIChecked}}
        <input type="radio" :checked="amIChecked">
      </label> &nbsp;
      <label>Recalculate Computed Value
        <button type="button" @click="mutateComputedDependentData">Click Me Several Times</button>
      </label>
    </div>
    <br>
    <div>
      <span>Simple Radio Group - v-model bound value = {{checkedGroupVModel}}</span><br>
      <label>Simple Radio 1:
        <input type="radio" name="vModelGroup" value="radioVModel1" v-model="checkedGroupVModel">
      </label><br>
      <label>Simple Radio 2:
        <input type="radio" name="vModelGroup" value="radioVModel2" v-model="checkedGroupVModel">
      </label><br>
      <label>Simple Radio 3:
        <input type="radio" name="vModelGroup" value="radioVModel3" v-model="checkedGroupVModel">
      </label>
    </div>
    <br>
    <div>
      <span>Simpe Radio - click handler to toggle data bound to :checked to toggle selection</span><br>
      <label>Toggle Radio = {{toggleChecked}}
        <input type="radio" :checked="toggleChecked" @click='onToggle()'>
      </label>
    </div>
  </div>
</div>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrniocenaView Question on Stackoverflow
Solution 1 - vue.jsPantelis PeslisView Answer on Stackoverflow
Solution 2 - vue.jsAleph-nullView Answer on Stackoverflow
Solution 3 - vue.jsleaveme_aloneView Answer on Stackoverflow
Solution 4 - vue.jsPhilip BijkerView Answer on Stackoverflow
Solution 5 - vue.jssteven87vtView Answer on Stackoverflow