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!
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.
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.
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.
<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.
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)" />
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)" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With