Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checked attribute not working in mat-radio-button

I am creating a radio button with the checked property but it's not showing selected

<mat-radio-group name="radioOpt1" [(ngModel)]="selectedRadio" [ngModelOptions]="{standalone: true}" (change)="radioChange($event)">
<mat-radio-button value="own" checked name="own">Own</mat-radio-button>
<mat-radio-button value="competitor" name="own">competitor</mat-radio-button> </mat-radio-group>

I want the first radio button to be checked by default

like image 442
D3v30 Avatar asked Feb 06 '19 13:02

D3v30


People also ask

How do you check radio button is checked or not?

Using Input Radio checked property: The Input Radio checked property is used to return the checked status of an Input Radio Button. Use document. getElementById('id'). checked method to check whether the element with selected id is check or not.

How do I make radio buttons not checked by default?

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> .


1 Answers

If using ngModel then you need to pass value of radio-button to ngModel.

<mat-radio-group name="radioOpt1" [(ngModel)]="selectedRadio" 
   [ngModelOptions]="{standalone: true}" (change)="radioChange($event)">
  <mat-radio-button value="own" name="own">Own</mat-radio-button>
  <mat-radio-button value="competitor" name="own">competitor</mat-radio-button> 
</mat-radio-group>

and ts file

 selectedRadio = 'own'; //default value

 radioChange(e){
   console.log(this.selectedRadio)
 }

or dynamically populated

 <mat-radio-group name="radioOpt1" [(ngModel)]="selectedRadio"
    [ngModelOptions]="{standalone: true}" (change)="radioChange($event)">
   <mat-radio-button *ngFor="let but of list" [value]="but.id" name="own" >
      {{but.name}}
   </mat-radio-button>
 </mat-radio-group>

ts file

  list = [{ "name": "own", id: "own"},{ "name": "competitor", id: "competitor"}];

  selectedRadio =this.list[0].id;
like image 102
Nenad Radak Avatar answered Oct 07 '22 05:10

Nenad Radak