How do I record that this ionic checkbox has been checked by the user?

36 Views Asked by At

I am trying to record that the user has checked the checkbox, and am not sure where to start. this is using the ionic framework with vanilla JS.

I have the tag for a checkbox:

<ion-list>
       <ion-checkbox id="Email-check">Email</ion-checkbox>
<ion-list>

A variable that is refering to the ID in the tag:

const emailCheckbox = this.querySelector('#Email-check'); And an empty function:

function storeemailCheck(){
        
    }
2

There are 2 best solutions below

0
Mikkel Holmberg On

As per the documentation there is a checked attribute on the ion-checkbox element, so you should be able to target that attribute using emailCheckbox.checked inside your function.

function storeemailCheck(){
    const emailCheckbox = this.querySelector('#Email-check');
    if (emailCheckbox.checked){
        // do something here..
    }
}
0
toTheBestOfMyKnowledge On

Use ngModel Template Driven forms.

<ion-checkbox slot="end" color="primary" [(ngModel)]="emailCheckbox" (ngModelChange)="someFunctionnInTs()"  ></ion-checkbox>

emailCheckbox will be true if user checks the checkbox and false if unchecked.If you need a change event you can call ngModelChange