Reputation: 137
I am trying to fetch the checked value of group of checkboxes in a table. I have a situation like
<td><input type="checkbox" value="{{my.id}}" /></td>
I have a checkbox at table header to toggle true/false for all table checkboxes
<th><input type="checkbox" id="checkAll" (click)="changeCheck()" [(ngModel)]="checkAllValue" /></th>
My code in changeCheck() is as below:
changeCheck(): void {
var checkedItems = jQuery("#tbPayments input[type='checkbox'][id!=checkAll]");
for (let item = 0; item < checkedItems.length; item++) {
console.log(checkedItems[item].checked = true);
}
}
But typescript throwing an error : Property 'checked ' does not exist on type 'HTMLElement'
How do i toggle list of checkboxes in my table. Could someone help !
Upvotes: 2
Views: 16485
Reputation: 18895
CheckedItems is typed to the most typed value that it can determine from the jQuery call, HTMLElement. If you know it is an HTMLInputElement, you should cast it as such:
changeCheck(): void {
var checkedItems = jQuery("#tbPayments input[type='checkbox'][id!=checkAll]") as HTMLInputElement[];
for (let item = 0; item < checkedItems.length; item++) {
console.log(checkedItems[item].checked = true);
}
}
Upvotes: 2
Reputation: 40554
Cast HTMLElement
to HTMLInputElement
which has the checked
property.
for (let item = 0; item < checkedItems.length; item++) {
console.log((checkedItems[item] as HTMLInputElement).checked = true);
}
Upvotes: 4