To check or uncheck an HTML checkbox in vanilla JavaScript, set its boolean checked DOM property directly (checkbox.checked = true or checkbox.checked = false). Setting .checked updates the live toggle state in the DOM, whereas setAttribute("checked", ...) / removeAttribute("checked") only changes the initial HTML attribute.
1. Setting the checked Property Directly
Query the <input type="checkbox"> element (for example with document.getElementById() or document.querySelector()) and assign a boolean to .checked:
// Check or uncheck a checkbox safely in vanilla JavaScript
function setCheckboxChecked(selector, isChecked) {
const el = document.querySelector(selector);
if (!(el instanceof HTMLInputElement) || el.type !== "checkbox") {
return false;
}
el.checked = Boolean(isChecked);
return el.checked;
}
setCheckboxChecked("#accept-terms", true); // Checks the box
setCheckboxChecked("#accept-terms", false); // Unchecks the box
el.checked = true / false: Directly updates the live boolean state of the checkbox in the browser.- Type guard (
instanceof HTMLInputElementandel.type === "checkbox"): Prevents accidental runtime errors if the selector matches a missing node or another element type.
2. Dispatching a change Event After Programmatic Updates
Changing checkbox.checked via JavaScript does not automatically fire a "change" event—dispatch one explicitly when other code or listeners need to react to the state change:
// Update checkbox state and notify any "change" event listeners
function updateCheckboxAndNotify(checkboxEl, isChecked) {
if (!(checkboxEl instanceof HTMLInputElement) || checkboxEl.type !== "checkbox") {
return;
}
checkboxEl.checked = Boolean(isChecked);
checkboxEl.dispatchEvent(new Event("change", { bubbles: true }));
}
const syncCheckbox = document.getElementById("auto-sync");
if (syncCheckbox) {
syncCheckbox.addEventListener("change", (event) => {
console.log("Auto-sync changed to:", event.currentTarget.checked);
});
updateCheckboxAndNotify(syncCheckbox, true);
}
dispatchEvent(new Event("change", { bubbles: true })): Triggers"change"listeners attached directly to the checkbox or via event delegation on an ancestor container.- Avoids duplicate work when no listeners exist: Use plain
el.checked = ...when you only want to set state quietly without firing"change".
3. Common Traps to Avoid
Updating checkbox state with vanilla JavaScript often runs into a few pitfalls:
- Using
setAttribute("checked", "true")orremoveAttribute("checked")to toggle live state:setAttribute("checked", "false")still evaluates to present in HTML and does not reliably uncheck an already-interacted checkbox—always useel.checked = false. - Expecting
el.checked = trueto fire"change"automatically: Programmatic property assignment is silent unless you callel.dispatchEvent(new Event("change", { bubbles: true })). - Forgetting
checkbox.indeterminate: If a checkbox shows a visual indeterminate dash (checkbox.indeterminate = true), settingcheckbox.checked = trueorfalsedoes not clear the indeterminate state unless you setcheckbox.indeterminate = false.
TL;DR: Assign a boolean to checkbox.checked (true to check, false to uncheck), and call checkbox.dispatchEvent(new Event("change", { bubbles: true })) when listeners need to react to the change.
Are you toggling a single checkbox, updating a group of checkboxes (like a “Select All” control), or triggering "change" listeners after programmatic updates?
