Poonam
Poonam

Reputation: 149

Make radio button checkd by default

I have implented 2 radio button for male and female and I want to make the first radio button checked by default.

I already added the checked property of radio button but still it is displaying unchecked

<div class="form-check form-check-inline">
   <input class="form-check-input" type="radio" name="gender" id="male" value="male" [(ngModel)]="data.gender" #gender="ngModel" [checked]>
   <label class="form-check-label" for="inlineRadio1">Male</label>
</div>
<div class="form-check form-check-inline">
   <input class="form-check-input" type="radio" name="gender" id="female" value="female" [(ngModel)]="data.gender" #gender="ngModel">
   <label class="form-check-label" for="inlineRadio2">Female</label>
</div>

Upvotes: 2

Views: 78

Answers (2)

rikg93
rikg93

Reputation: 1269

You can use [(ngModel)], but you'll need to update your value to data.gender with "male", remove [] from checked.

Upvotes: 0

Adrita Sharma
Adrita Sharma

Reputation: 22213

In your typescript,

Store data.gender = "male"

and remove checked attribute from HTML. By using above approach your checked value must be always updated [as shown in the example].

Live_Example

Upvotes: 2

Related Questions