Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

camiant's avatar

vuejs v-for radio list and checked value

I have a component and I need to loop through an array of values to create a radio list (v-for is perfect) and to check only a specific radio (by matching its value with another component data value). Need this operation to be performed on load.

comparisonvalue = 2

items =
[
{txt: 'foo', val: 1},
{txt: 'bar', val: 2}
]

<div  v-for="item in items">
    <input name="myfield" type="radio" v-bind:value="item.val" v-bind:checked="item.val==comparisonvalue">
    <label>{{ item.txt }}</label>
</div>

it doesn't work correctly ...

0 likes
4 replies
joedawson's avatar

Does this work?

<input name="myfield" type="radio" v-bind:checked="{ item.val == comparisonvalue }">

Untested

1 like
Juukie's avatar

Just use v-model.

<input type="radio" value="{{ item.val }}" v-model="comparisonvalue">

If you click another radio button, the comparisonvalue will be changed to the items value. See: http://vuejs.org/guide/forms.html#Radio. Good luck!

2 likes
welsper's avatar

Hello, Can you please help me, how can I get the value of a buch of checkboxes using vue?? my code is like this but not getting the right value Region: All EMEA US LAC APJ

abusalameh's avatar

in your javascript file add checked property ..

new Vue({
  el: '#app',
  data: {
            items : [{txt: 'foo', val: 1},{txt: 'bar', val: 2}],
      checked:'',
  }
})

in your html you must add :value to the rendered list as follow :)

<div id="app">
  <ul>
    <li v-for="item in items"> 
        <input type="radio" v-model="checked" :value="item.txt"> {{ item.txt }}
    </li>
  </ul>
  
  {{ checked }}
</div>
2 likes

Please or to participate in this conversation.