0

I have the next radio buttons in my html:

<div class="col-lg-4" id="radioButtons"> 
    <form action="">
        <fieldset id="capacity">
            <legend>capacity</legend>
            <label for="input-ao"><input size=25 id="input-ao" type="radio" name="capacity" [value]="0-30" checked="checked" />0-30</label>
            <label for="input-lo"><input id="input-lo" type="radio" name="capacity" [value]="30-60"/>30-60</label>
            <label for="input-lo"><input id="input-lo" type="radio" name="capacity" [value]="60-100"/>60-100</label>
            <label for="input-lo"><input id="input-lo" type="radio" name="capacity" [value]="100"/>100+</label>
            <br>
        </fieldset>
        <fieldset id="computerFieldset">
            <legend>computer</legend>
            <label for="input-ao"><input size=25 id="input-ao" type="radio" name="comp" value="yes" checked="checked" />yes</label>
            <label for="input-lo"><input id="input-lo" type="radio" name="comp" value="no"/>no</label>
            <br>
        </fieldset>
    </form>
</div>

I want to know which radio button is selected, also I want to know will it cause a problem that I have a default checked="checked"? Thank you.

Usman Maqbool
  • 3,351
  • 10
  • 31
  • 48
T.S
  • 55
  • 1
  • 10
  • You should not have 2 radio buttons with the same id - in your HTML structure. It will mess up the functionality, IMO. – Hunor Jun 12 '19 at 04:30
  • 1
    Have a look at the link :- https://stackoverflow.com/questions/31879497/angular2-radio-button-binding – Krishna Jun 12 '19 at 04:33

3 Answers3

1

You need to add [(ngModel)]="YourVariableFromTsCodeBehindFile" to each radio button of your radio group.

It should look like,

<div class="col-lg-4" id="radioButtons"> 
  <form action="">
    <fieldset id="capacity">
      <legend>capacity</legend>
      <label for="input-ao"><input [(ngModel)]="selectedRadioBtnFrom1stGroup" size=25 type="radio" name="capacity" value="0-30" checked="checked" />0-30</label>
      <label for="input-lo"><input [(ngModel)]="selectedRadioBtnFrom1stGroup" type="radio" name="capacity"  value="30-60"/>30-60</label>
      <label for="input-lo"><input [(ngModel)]="selectedRadioBtnFrom1stGroup" type="radio" name="capacity"  value="60-100"/>60-100</label>
      <label for="input-lo"><input [(ngModel)]="selectedRadioBtnFrom1stGroup" type="radio" name="capacity"  value="100"/>100+</label>
      <br>
      {{selectedRed}}
    </fieldset>

    <fieldset id="computerFieldset">
      <legend>computer</legend>
      <label for="input-ao"><input size=25 [(ngModel)]="selectedRadioBtnFrom2ndGroup" type="radio" name="comp" value="yes" checked="checked" />yes</label>
      <label for="input-lo"><input [(ngModel)]="selectedRadioBtnFrom2ndGroup" type="radio" name="comp" value="no"/>no</label>

      <br>
    {{blue.id}}   
    </fieldset>  
  </form>
</div>

Demo: https://stackblitz.com/edit/angular-material-radio-button-demo-ksrxbc?file=app/app.component.html

Ashokan Sivapragasam
  • 2,033
  • 2
  • 18
  • 39
0

You could use a changeHandler method, for example - as it is shown in this video: https://www.youtube.com/watch?v=Wo9nfK2fEyw

Example:

<label for="input-ao"><input size=25 id="input-ao" type="radio" name="capacity" [value]="0-30" checked="checked" (change)="radioChangeHandler($event)" />0-30</label>

radioChangeHandler(event: any){
    this.selectedRadioButton = event.target.value;
}
Hunor
  • 449
  • 1
  • 5
  • 19
0

You can make a function called radioChanged() And within this function you can console the value of the selected radio input

radioChange(event: any){console.log(event.target.value)}

Its just an idea. Because i have no laptop right now.. If you do not understand the concept let me know i will edit the answer

Wiki
  • 224
  • 6
  • 21