Amine
Amine

Reputation: 11

How to check only the first radio in vuejs v-for?

How to put inline if in checked property on input in the vuejs?

this is my code

<div v-for="(element,index) in elements" :key="element.id" class="col">
    <input type="radio" name="test[]" :value="element.id" :checked="if (index==0):'checked'">
</div>

Igot error message : avoid using JavaScript keyword as property name: "if" Raw expression: :checked="if (index==0) 'checked'"

Upvotes: 0

Views: 490

Answers (2)

John B
John B

Reputation: 11

this will be enough:

<input
   type="radio"
   :checked="index === 0"
/>

Upvotes: 1

Amine
Amine

Reputation: 11

This is how to do:

<div v-for="(element,index) in elements" :key="element.id" class="col">
    <input type="radio" name="test[]" :value="element.id" :checked="index===0 ? true: false">
</div>

Upvotes: 1

Related Questions