I have an angular2 application where I want to dynamically check or uncheck based on user action.
This is my functionality,
When I try to use this
document.getElementById("checkBoxID").checked = false;
I get a compilation error
Error:(33, 60) TS2339: Property 'checked' does not exist on type 'HTMLElement'.
I researched somewhere and found that this property could be 'selected' instead of 'checked', but even that doesn't work.
I can use a workaround of using *ngIf with a Boolean value and get two check-boxes written in my component, one with 'checked' and one with 'unchecked' and render them accordingly based on the Boolean value and keep setting the Boolean value on click of buttons, but I believe that would be the dirty solution.
Angular2 for sure would have something simpler to do this, which I am missing here :(
Two possible ways
<button (click)="cb.click()">toggle</button>
<input type="checkbox" #cb>
<button (click)="isChecked = !isChecked">toggle</button>
<input type="checkbox" [(ngModel)]="isChecked">
Plunker example
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