Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining if checkbox is checked in Ionic 2

How can I determine if a ion-checkbox is checked in Ionic 2.

   <ion-item *ngFor="let a of q.Answers">
      <ion-label>{{a.AnswerDescription}}</ion-label>
      <ion-checkbox (click)="addValue($event)"></ion-checkbox>
   </ion-item>

the following return undefined

 addValue(e): void {
     var isChecked = e.currentTarget.checked;
     console.log(isChecked);//undefined

 }
like image 345
Arianule Avatar asked Oct 30 '16 11:10

Arianule


People also ask

How do I select only one checkbox in ionic?

var chk1 = document. getElementById("isChecked1"); var chk2 = document. getElementById("isChecked2"); chk1.

How do I make an ionic checkbox?

The Ionic checkboxes are styled differently on each platform as like other Ionic components. You can use checked attribute with the <ion-checkbox> element to set the default value, and disabled attribute to disable the user from changing the value.


2 Answers

Use e.checked property if you really wanted to pass $event as an argument as shown below.

HTML

<ion-checkbox color="dark" checked="true" (ionChange)="datachanged($event)" ></ion-checkbox>

TYPESCRIPT

datachanged(e:any){
    console.log(e);
    console.log(e.checked);
}
like image 128
Bhavik Patel Avatar answered Oct 09 '22 21:10

Bhavik Patel


Just use an NgModel

<ion-checkbox checked="false" [(ngModel)]="yourVariable.checked"></ion-checkbox>
like image 26
Franco Avatar answered Oct 09 '22 19:10

Franco