Born2Code
Born2Code

Reputation: 137

How to Set checkbox properties in typescript

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>

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

Answers (2)

Daryl
Daryl

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

Saravana
Saravana

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

Related Questions