Check or uncheck a checkbox with JavaScript
Codemia Knowledge Hub ·
To check or uncheck a checkbox in JavaScript, set its checked property to true or false. Use the DOM property, not the HTML attribute: user clicks change the property at runtime, while the attribute keeps its initial markup value.
Checking and unchecking a single checkbox
Start with a checkbox in HTML:
Get the element and set checked:
This is the standard browser API. It works in every modern browser with no libraries. Make sure the script runs after the element exists: in the <head> without defer or a DOMContentLoaded handler, getElementById returns null.
Toggling a checkbox
Negate the property to flip the current state:
This is useful when another control drives the checkbox, such as a button or a keyboard shortcut handler:
Toggling from JavaScript does not fire the change event. If your code relies on change listeners, dispatch the event yourself, as shown next.
Reacting to user changes with the change event
When a user clicks a checkbox, the browser fires change:
This is the standard pattern for forms, feature toggles, consent banners and filter UIs. By default change fires only on user interaction. If you set checked in code and need the listener to run, dispatch the event:
Checking or unchecking a group of checkboxes
Query a group together instead of handling each element:
Check all of them:
Uncheck all of them:
Collect the values of the checked boxes only:
The :checked pseudo-selector lets querySelectorAll select by current state, so you do not have to loop and inspect each element.
Select all / deselect all
A "select all" checkbox controlling a group of child checkboxes is common in admin dashboards, email clients and bulk-action tables:
The second listener is the one people forget. Without it, "select all" stays checked after a user unchecks a single row, so the UI shows the wrong state.
Why setAttribute('checked') is wrong
A common mistake:
This changes the HTML attribute, not the DOM property. Once a user interacts with the checkbox, the two diverge: the checked property tracks the live state, while the attribute only reflects the initial value from markup.
| Approach | Controls | Tracks User Interaction |
checkbox.checked = true | DOM property | Yes |
checkbox.setAttribute('checked', '') | HTML attribute | No |
checkbox.removeAttribute('checked') | HTML attribute | No |
Use the property for runtime behavior. The attribute only matters for the initial HTML render.
Requiring a checkbox before form submission
A typical use is requiring a terms-of-service checkbox before the form submits:
This reads the live checked property right before the form submits. For simple cases HTML can do this without JavaScript: the required attribute enforces the checkbox on submission, and a <label> already toggles its associated checkbox. Reach for JavaScript only when you need more than that.
Dynamically rendered checkboxes
When checkboxes are rendered dynamically (client-side templates, API responses, framework rendering), watch for stale references. The NodeList returned by querySelectorAll is static, so checkboxes added to the DOM later will not be in a cached result.
One fix is to re-query inside the event handler:
The other is event delegation on a parent container:
Event delegation is usually the better choice for dynamic content because it scales without re-binding listeners.
Related reading
- Chess game in JavaScript
- Choosing between Thymeleaf and Angular for a new Spring MVC project
- Chrome Extensions synchronous calls - create window only after window
- Clearing localStorage in javascript?
- Client network socket disconnected before secure TLS connection was established. How can I connect to a kafka cluster using Kafka JS in Node js?
- Colors in JavaScript console
- Combine json arrays by key, javascript
- Compare Strings Javascript Return of Likely
System design interview problems on this topic
- Design Google Doc: solution and walkthrough
- Design Facebook Messenger: solution and walkthrough
- Design Typeahead Suggestion: solution and walkthrough
- Design an Online Presence Indicator Service: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.