GLM 4.5 Air

jQuery Checkbox: How to Check/Uncheck Guide

Learn the correct jQuery methods to check or uncheck checkboxes. Discover why .prop() works better than .checked() or .selected(). Complete guide with examples and troubleshooting.

Question

How to check or uncheck a checkbox using jQuery?

I’m trying to programmatically check a checkbox using jQuery, but the following approaches don’t seem to work:

javascript
$(".myCheckBox").checked(true);
javascript
$(".myCheckBox").selected(true);

What is the correct jQuery method to check or uncheck a checkbox element?

GLM 4.5 Air

How to Check or Uncheck a Checkbox Using jQuery

The correct way to check or uncheck a checkbox with jQuery is to use the .prop() method, not .checked() or .selected(). Here’s the basic approach:

javascript
// Check a checkbox
$(".myCheckBox").prop("checked", true);

// Uncheck a checkbox
$(".myCheckBox").prop("checked", false);

Contents


Basic Checkbox Manipulation with jQuery

jQuery provides several methods to manipulate checkboxes. The most reliable approach is using .prop():

Checking a Checkbox

javascript
// Select by class and check
$(".myCheckBox").prop("checked", true);

// Select by ID and check
$("#myCheckboxId").prop("checked", true);

// Select by name attribute and check
$("input[name='myCheckboxName']").prop("checked", true);

Unchecking a Checkbox

javascript
// Select by class and uncheck
$(".myCheckBox").prop("checked", false);

// Select by ID and uncheck
$("#myCheckboxId").prop("checked", false);

// Select by name attribute and uncheck
$("input[name='myCheckboxName']").prop("checked", false);

Toggling a Checkbox

javascript
// Toggle the state of a checkbox
$(".myCheckBox").prop("checked", function(i, value) {
    return !value;
});

Understanding the Difference Between .prop() and .attr()

Many jQuery developers are confused about when to use .prop() versus .attr() for checkboxes. Here’s the key difference:

Using .prop() (Recommended)

javascript
// Preferred method for boolean properties like 'checked'
$(".myCheckBox").prop("checked", true);

The .prop() method is designed to handle DOM properties. The checked property is a boolean value that reflects the current checked state of the checkbox.

Using .attr() (Not Recommended)

javascript
// Works but not recommended for checkboxes
$(".myCheckBox").attr("checked", "checked");

The .attr() method sets HTML attributes. While this can work for checkboxes, it’s not as reliable as .prop() because attributes and properties can sometimes get out of sync, especially with dynamic content.

Why Your Original Code Didn’t Work

javascript
$(".myCheckBox").checked(true);    // Incorrect - no such jQuery method
$(".myCheckBox").selected(true);   // Incorrect - no such jQuery method

jQuery doesn’t have methods named .checked() or .selected(). These are native JavaScript properties, not jQuery methods.


Handling Multiple Checkboxes

When working with multiple checkboxes, you’ll often need to:

Check All Checkboxes

javascript
// Check all checkboxes with a specific class
$(".checkboxClass").prop("checked", true);

// Check all checkboxes on the page
$("input[type='checkbox']").prop("checked", true);

Uncheck All Checkboxes

javascript
// Uncheck all checkboxes with a specific class
$(".checkboxClass").prop("checked", false);

// Uncheck all checkboxes on the page
$("input[type='checkbox']").prop("checked", false);

Get Checked Checkboxes

javascript
// Get all checked checkboxes
var checkedBoxes = $("input[type='checkbox']:checked");

// Loop through checked checkboxes
checkedBoxes.each(function() {
    console.log($(this).val());
});

Event Handling for Checkbox Changes

You can respond to checkbox changes using jQuery’s event handlers:

Basic Change Event

javascript
$(".myCheckBox").change(function() {
    if($(this).prop("checked")) {
        console.log("Checkbox is checked");
    } else {
        console.log("Checkbox is unchecked");
    }
});

Click Event (Alternative)

javascript
$(".myCheckBox").click(function() {
    console.log("Checkbox was clicked. Current state: " + $(this).prop("checked"));
});

Handling Change with Delegation

For dynamically added checkboxes:

javascript
$(document).on("change", ".dynamicCheckbox", function() {
    console.log("Dynamic checkbox changed: " + $(this).prop("checked"));
});

Practical Examples and Use Cases

Example 1: Form Validation

javascript
// Check if at least one checkbox is checked
function validateCheckboxGroup() {
    if ($(".requiredCheckbox:checked").length === 0) {
        alert("Please select at least one option");
        return false;
    }
    return true;
}

// Usage in form submission
$("#myForm").submit(function(e) {
    if (!validateCheckboxGroup()) {
        e.preventDefault();
    }
});

Example 2: Check All / Uncheck All Functionality

javascript
// Check all when "Check All" is clicked
$("#checkAll").click(function() {
    $(".itemCheckbox").prop("checked", true);
});

// Uncheck all when "Uncheck All" is clicked
$("#uncheckAll").click(function() {
    $(".itemCheckbox").prop("checked", false);
});

// Update "Check All" state when individual checkboxes change
$(".itemCheckbox").change(function() {
    if ($(".itemCheckbox:not(:checked)").length === 0) {
        $("#checkAll").prop("checked", true);
    } else {
        $("#checkAll").prop("checked", false);
    }
});

Example 3: Toggle Related Checkboxes

javascript
// When a main category checkbox is checked/unchecked, toggle all sub-items
$(".categoryCheckbox").change(function() {
    var categoryId = $(this).data("category");
    // Check or uncheck all related checkboxes
    $(".subcategory-checkbox[data-category='" + categoryId + "']")
        .prop("checked", $(this).prop("checked"));
});

Common Mistakes and Troubleshooting

Mistake 1: Using .val() Instead of .prop()

javascript
// Incorrect - .val() doesn't work for checking/unchecking
$(".myCheckBox").val(true);

// Correct
$(".myCheckBox").prop("checked", true);

Mistake 2: Not Using Boolean Values

javascript
// Incorrect - using string values
$(".myCheckBox").prop("checked", "true");

// Correct - using boolean values
$(".myCheckBox").prop("checked", true);

Mistake 3: Not Waiting for DOM Ready

javascript
// Incorrect - code might run before DOM is ready
$(".myCheckBox").prop("checked", true);

// Correct - wrap in document ready handler
$(document).ready(function() {
    $(".myCheckBox").prop("checked", true);
});

// Or use the shorthand
$(function() {
    $(".myCheckBox").prop("checked", true);
});

Troubleshooting Checklist

  • [ ] Verify your selector is correctly targeting the checkbox
  • [ ] Ensure the code runs after the DOM is ready
  • [ ] Use boolean values (true/false) not strings
  • [ ] Check for JavaScript errors in console
  • [ ] Verify the checkbox isn’t being modified by other scripts

Conclusion

To properly check or uncheck checkboxes with jQuery:

  1. Use .prop("checked", true) to check a checkbox
  2. Use .prop("checked", false) to uncheck a checkbox
  3. Avoid .checked() and .selected() methods as they don’t exist in jQuery
  4. For multiple checkboxes, use .length property to count checked items
  5. Handle events with .change() or .click() methods

The key takeaway is understanding that checkboxes are controlled by DOM properties, not attributes, which is why .prop() is the preferred method over .attr() for this task.