How to set "checked" for a checkbox using jQuery: a detailed breakdown for beginners

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Why does the checkbox go crazy? The story of one bug

\n\n

Imagine: you are writing a registration form on a website. There is a checkbox "I agree to the terms." The user clicks — the checkmark appears. Everything works. Then you decide to add a "Select all" button using jQuery. You write $('#agree').attr('checked', true) — and... nothing happens. Or the checkmark appears once, and then stops being removed. Sound familiar?

\n\n

This question is one of the most popular on Stack Overflow. Thousands of developers stumble over the same rake. And today we will figure out why this happens, and how to correctly set checked for a checkbox using jQuery.

\n\n

In short: what's the problem?

\n\n

The problem is that many beginners (and not only) use the .attr() method to manage the checked attribute. But starting with jQuery 1.6, the correct way is to use .prop(). The difference between an attribute and a property is a key topic that we will now break down.

\n\n

Attribute vs Property: what's the difference?

\n\n

Let's put it simply. An HTML attribute is what you write in the markup:

\n\n
<input type="checkbox" id="agree" checked>
\n\n

The checked attribute here sets the initial state of the checkbox. But when the user clicks, the property of the DOM element changes, while the attribute remains unchanged.

\n\n

The .attr() method works with attributes, while .prop() works with properties. And since we are interested in the current state of the checkbox (which changes dynamically), we need .prop().

\n\n

What it looks like in code

\n\n

Here is a simple example that demonstrates the difference:

\n\n
<!-- HTML -->\n<input type="checkbox" id="myCheckbox">\n<button id="setChecked">Set checkmark</button>\n<button id="removeChecked">Remove checkmark</button>\n\n<script>\n// Correct way (jQuery 1.6+)\n$('#setChecked').click(function() {\n    $('#myCheckbox').prop('checked', true);\n});\n\n$('#removeChecked').click(function() {\n    $('#myCheckbox').prop('checked', false);\n});\n</script>
\n\n

Note: we pass true or false as the second argument — a boolean value. This is important.

\n\n

Typical beginner mistakes (and how to avoid them)

\n\n

Mistake 1: Using .attr() to set checked

\n\n
// ❌ Incorrect\n$('#myCheckbox').attr('checked', true);\n\n// ❌ Also incorrect\n$('#myCheckbox').attr('checked', 'checked');
\n\n

Why is this bad? Because .attr() sets the attribute, not the property. After the user's first click, the attribute may remain, but the property changes. The result is inconsistent behavior.

\n\n

Mistake 2: Passing a string instead of a boolean value

\n\n
// ❌ Incorrect\n$('#myCheckbox').prop('checked', 'true'); // string, not boolean
\n\n

Does jQuery convert a string to a boolean? In some cases — yes. But relying on this is bad practice. Always pass true or false without quotes.

\n\n

Mistake 3: Forgetting about .prop() when reading the state

\n\n
// ❌ Incorrect\nif ($('#myCheckbox').attr('checked')) { ... }\n\n// ✅ Correct\nif ($('#myCheckbox').prop('checked')) { ... }
\n\n

For reading the current state — only .prop().

\n\n

Complete example: a form with checkboxes and control buttons

\n\n

Let's put it all together. Imagine we have a list of options that the user can select, and "Select all" and "Deselect all" buttons.

\n\n
<!DOCTYPE html>\n<html>\n<head>\n    <title>Managing checkboxes with jQuery</title>\n    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>\n</head>\n<body>\n    <h3                

Blogs

Book Recommendations