Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to change the check status of a checkbox in TypeScript?

I have my input below:

<input class="form-check-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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

So how should I proceed?

>Solution :

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;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading