Join the AI Workshop and learn to build real-world apps with AI. A hands-on, practical program to level up your skills.
Use the checked property of the checkbox element. Example:
<input type="checkbox" class="checkbox" />
To check whether it is checked:
document.querySelector('.checkbox').checked
Alternatively, check whether a checked checkbox exists:
document.querySelector('.checkbox:checked') !== null
Using the .checked property is usually clearer.
Do not use getAttribute('checked') to determine checked state; the attribute does not reflect user toggles. For example:
<input type="checkbox" checked />
Do not rely on the checkbox’s value for checked state—it is always on when present.