zxc
zxc

Reputation: 1526

Angular 8 Checkbox Change Event Checked undefined

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!

Upvotes: 14

Views: 39404

Answers (2)

Mayur Parmar
Mayur Parmar

Reputation: 264

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)" />

Upvotes: 3

Vivek Doshi
Vivek Doshi

Reputation: 58523

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)" />

Upvotes: 25

Related Questions