Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 8 Checkbox Change Event Checked undefined

Tags:

angular

Im trying to execute a function when a checkbox has been ticked/unticked but wasnt able to get the checkbox.checked as it is showing as undefined.

html:

<input type="checkbox" (change)="eventCheck($event)" />

typescript:

eventCheck(event){
console.log(event.checked) <--- this is undefined
}

note: I was able to get the event object but im not sure which property to check if the checkbox has been checked or not.

Can you guys help me with this? thanks!

like image 373
zxc Avatar asked Aug 31 '19 13:08

zxc


People also ask

How do you check the checkbox is checked or not in angular 8?

The ng-checked Directive in AngularJS is used to read the checked or unchecked state of the checkbox or radio button to true or false. If the expression inside the ng-checked attribute returns true then the checkbox/radio button will be checked otherwise it will be unchecked.

How do you check checkbox is checked or not in TypeScript?

To check if a checkbox element is checked in TypeScript: Type the element as HTMLInputElement using a type assertion. Use the checked property to see if the element is checked. The property will return true if it is checked and false otherwise.

How do you check whether the checkbox is checked or not in angular?

Just define an ng-model directive in the checkbox and to find checkbox checked or not check model return value (TRUE or FALSE). If it has TRUE means checkbox is been checked.

What is indeterminate in Mat checkbox?

<mat-checkbox> supports an indeterminate state, similar to the native <input type="checkbox"> . While the indeterminate property of the checkbox is true, it will render as indeterminate regardless of the checked value. Any interaction with the checkbox by a user (i.e., clicking) will remove the indeterminate state.


2 Answers

it should be event.target try :

eventCheck(event){
    console.log(event.target.checked) <--- Check with this
}

OR Use it like :

<input type="checkbox" (change)="eventCheck($event.target)" />
like image 156
Vivek Doshi Avatar answered Oct 03 '22 21:10

Vivek Doshi


Here Is Solution...

Chek.ts

testCheck(event){
       console.log(event.target.checked); <--- Return True/False Check/UnCheck
}

Chek.html

<input type="checkbox" name="test" (change)="testCheck($event)" />
like image 27
Mayur Parmar Avatar answered Oct 03 '22 20:10

Mayur Parmar