jquery
javascript

How do I check whether a checkbox is checked in jQuery?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

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:

html
<input type="checkbox" id="newsletter">
javascript
1if ($('#newsletter').is(':checked')) {
2    console.log('checked');
3} else {
4    console.log('not checked');
5}

The equivalent property-based version is:

javascript
if ($('#newsletter').prop('checked')) {
    console.log('checked');
}

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.

javascript
console.log($('#newsletter').attr('checked'));
console.log($('#newsletter').prop('checked'));

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:

html
<input type="checkbox" id="agree">
<button id="submitBtn" disabled>Submit</button>
javascript
1$('#agree').on('change', function () {
2    const checked = $(this).is(':checked');
3    $('#submitBtn').prop('disabled', !checked);
4});

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:

html
<input type="checkbox" class="feature" value="a" checked>
<input type="checkbox" class="feature" value="b">
<input type="checkbox" class="feature" value="c" checked>
javascript
const count = $('.feature:checked').length;
console.log(count);

If you need their values:

javascript
1const values = $('.feature:checked').map(function () {
2    return this.value;
3}).get();
4
5console.log(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:

html
1<form id="signupForm">
2    <input type="checkbox" id="terms">
3    <button type="submit">Create account</button>
4</form>
javascript
1$('#signupForm').on('submit', function (event) {
2    if (!$('#terms').is(':checked')) {
3        event.preventDefault();
4        alert('You must accept the terms.');
5    }
6});

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:

javascript
$(document).on('change', '.dynamic-checkbox', function () {
    console.log($(this).prop('checked'));
});

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 change or submit handlers when behavior depends on user input.
  • For groups of checkboxes, $('.class:checked') is the cleanest pattern.

Course illustration
Course illustration

All Rights Reserved.