How do I check whether a checkbox is checked in jQuery?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In jQuery, the usual way to test a checkbox is is(':checked') or prop('checked'). Both inspect the current DOM state, which is what you want when the user may have changed the checkbox after page load.
The Two Standard Patterns
For a single checkbox, the most readable form is usually:
The equivalent property-based version is:
In everyday jQuery code, either approach is acceptable. is(':checked') reads more like a question, while prop('checked') feels more explicit about reading a DOM property.
Why attr('checked') Is The Wrong Tool
Older examples sometimes use attr('checked'), but that reads the HTML attribute rather than the live checkbox state.
If the user clicks the checkbox, the property changes immediately. The original HTML attribute may not reflect that interactive state.
That is why modern jQuery guidance is:
- use
:checked - or use
.prop('checked') - avoid
.attr('checked')for state checks
Check The State During Events
You often want to read the checkbox inside a change handler:
This is a common pattern for:
- enabling submit buttons
- turning feature panels on and off
- filtering UI state
- reacting to consent or preference toggles
The important detail is that you are reading the current state at the moment of the event.
Working With Multiple Checkboxes
If you need to know how many checkboxes are checked, use the selector directly:
If you need their values:
That is clearer than manually looping through every checkbox and inspecting each one separately.
Form Validation Example
Checking the box state during form submission is another common case:
This is a good example of why you should inspect the live state instead of assuming the original markup tells you what the user did.
If you are dealing with dynamically inserted checkboxes, the same checking logic still applies, but the event binding may need delegation:
That matters in older jQuery-heavy applications where rows or forms are added after the initial page render.
It also makes debugging easier because delegated handlers confirm that the checkbox exists at the time the event fires, not only at initial page load.
Common Pitfalls
One common mistake is using attr('checked') and then wondering why the result does not follow user clicks.
Another issue is calling .is(':checked') on a selector that matches nothing. In that case the result is false, which may hide the fact that the element was never found.
A third problem is checking the box state before the DOM is ready or before the element has been inserted dynamically.
Finally, in modern codebases that do not already use jQuery, adding jQuery solely for checkbox checks is unnecessary because plain DOM APIs handle this easily too.
Summary
- Use
$('#id').is(':checked')for a readable single-checkbox test. - Use
.prop('checked')when you want the live boolean property explicitly. - Avoid
.attr('checked')for interactive state. - Read the checkbox state inside
changeorsubmithandlers when behavior depends on user input. - For groups of checkboxes,
$('.class:checked')is the cleanest pattern.
Related reading
- How do I disable the resizable property of a textarea?
- How do I format a date in JavaScript?
- How do I get a timestamp in JavaScript?
- How do I include a JavaScript file in another JavaScript file?
- How do I make the first letter of a string uppercase in JavaScript?
- How do I replace all occurrences of a string in JavaScript?
- How to access the index value in a 'for' loop?
- How to disable text selection highlighting
.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 courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.