14

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!

2
  • be carefull, if you use javaScript must be evet,target.checked, if use material angular is event.checked, if use (ngModelChange) $event is true or false.tip: use console.log(event) and see what are you receiving Commented Aug 31, 2019 at 13:53
  • thanks for the tip eliseo! Commented Aug 31, 2019 at 14:24

2 Answers 2

24

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)" />
Sign up to request clarification or add additional context in comments.

2 Comments

this works @vivek thank you! ill tag this as accepted answer after 5mins
What is the type of that event parameter? Event interface does not have target.checked field.
3

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.