Sometimes, we want to determine if a checkbox is checked with JavaScript.
In this article, we’ll look at how to determine if a checkbox is checked with JavaScript.
How to determine if a checkbox is checked with JavaScript?
To determine if a checkbox is checked with JavaScript, we use the checked
property.
For instance, we write
<label><input id="lifecheck" type="checkbox" />Lives</label>
to add a checkbox.
Then we write
const checked = document.getElementById("lifecheck").checked;
to select the checkbox with getElementById
.
And then we check if it’s checked with the checked
property.
Conclusion
To determine if a checkbox is checked with JavaScript, we use the checked
property.