Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting selected radio button value, on (ionChange) in radio-group , ionic2

Tags:

ionic2

I want to get the selected radio button value, on (ionChange) event in radio-group in Ionic2.

My Html code is

<ion-list radio-group *ngFor="let question of mcqData; let i= index;" (ionChange)="mcqAnswer(i)">
  <ion-list-header>
    {{question.questionText}}
  </ion-list-header>

  <ion-item>
    <ion-label>{{question.optionA}}</ion-label>
    <ion-radio  value="1"></ion-radio>
  </ion-item>

  <ion-item>
    <ion-label>{{question.optionB}}</ion-label>
    <ion-radio  value="2"></ion-radio>
  </ion-item>

  <ion-item>
    <ion-label>{{question.optionC}}</ion-label>
    <ion-radio  value="3"></ion-radio>
  </ion-item>

  <ion-item>
    <ion-label>{{question.optionD}}</ion-label>
    <ion-radio  value="4"></ion-radio>
  </ion-item>
</ion-list>

How can I get ion-radio value in mcqQuesiton() on (ion-change).

I have number or radio-groups on a single page, as it is a multiple choice question page.

like image 629
raju Avatar asked Sep 13 '16 08:09

raju


People also ask

How do you use ion radio-group?

Radio components are generally used as a set of related options inside of a group, but it can also be used alone. A radio button options can be checked by selecting it or checked programmatically by setting the checked property. It also uses a disabled attribute to disable the user from changing the value.

How do you reset an ion radio button?

Solution for IONIC 5: i) define radio group reference. ii) add a click event with radio group. It will reset the radio button also the model/formcontrol too.

How do I test my ion radio?

Pressing on a radio will check it. They can also be checked programmatically by setting the checked property. An ion-radio-group can be used to group a set of radios. When radios are inside of a radio group , only one radio in the group will be checked at any time.


1 Answers

You can use ionSelect event of <ion-radio> to pass the value.

...
  <ion-item>
    <ion-label>{{question.optionA}}</ion-label>
    <ion-radio  value="1" (ionSelect)="mcqAnswer(i,1)"></ion-radio>
  </ion-item>
  <ion-item>
    <ion-label>{{question.optionB}}</ion-label>
    <ion-radio  value="2" (ionSelect)="mcqAnswer(i,2)"></ion-radio>
  </ion-item>
...

In your typescript (.ts) file it will be something like

mcqAnswer(questionID,answer){
...
}
like image 116
AwesomeGeek Avatar answered Sep 20 '22 08:09

AwesomeGeek