Home > Back-end >  How to change the check status of a checkbox in TypeScript?
How to change the check status of a checkbox in TypeScript?

Time:01-10

I have my input below:

<input  type="checkbox" [name]="myCheck">

I want to get the element in TypeScript file and change in it some sort of like:

document.getElementByName("myCheck").checked = true;

But the 'checked' property is not recognizable.

So how should I proceed?

CodePudding user response:

You're using getElementByName, but that's not a valid function. It should be getElementsByName and then give a valid index.

You can also use querySelector("[name='myCheck']").

As for typescript:

You first have to let typescript know what the element exactly is:

const checkboxEl = document.getElementsByName("myCheck")[0] as HTMLInputElement;
checkboxEl.checked = true;

CodePudding user response:

getElementsByName return the collection either use the ID or access the first element

 document.getElementsByName("myCheck")[0].checked = true;
  • Related