Usually checked
just add at the end of input
<input type="radio" name="item" value="value1" checked>
<input type="radio" name="item" value="value2">
But now I am using *ngFor. I want to select the first one.
I try to do this, but it does not work, because checked
is not a class.
<div *ngFor="#item of collection; #i = index"">
<input type="radio" name="item" value="{{item}}" [ngClass]="{'checked':i === 0}">
<label>{{item}}</label>
</div>
So how can I do it? Thanks!
The checked property sets or returns the checked state of a radio button. This property reflects the HTML checked attribute.
You can check a radio button by default by adding the checked HTML attribute to the <input> element. You can disable a radio button by adding the disabled HTML attribute to both the <label> and the <input> .
Value. The value attribute is a string containing the radio button's value. The value is never shown to the user by their user agent. Instead, it's used to identify which radio button in a group is selected.
<input type="radio" name="item" value="{{item}}" [attr.checked]="i === 0 ? '' : null">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With