How to Check if a Checkbox is Selected in jQuery: A Step-by-Step Tutorial for Beginners
\n\nHi there, friend! If you've ever worked with forms on a web page, you've probably encountered the task: "Is this checkbox selected?". It seems like a small thing, but it's precisely these small things that often stump even experienced developers. On Stack Overflow, the question "How do I check whether a checkbox is checked in jQuery?" has gathered thousands of views, which means this topic is really hot.
\n\nIn this tutorial, I won't just show you dry syntax. We'll break down several methods, compare them, find out where the pitfalls are, and write clean, working code. Let's go!
\n\nWhy is this important? (And what does jQuery have to do with it?)
\nToday, many are switching to vanilla JavaScript, but jQuery still lives in millions of projects: from old websites to modern admin panels. Knowing how to work with :checked and the .prop() / .attr() methods is a fundamental skill that will save you hours of debugging.
Imagine: you're building a registration form where you need to agree to the terms. Or an online store with product filters. You can't get anywhere without checking the checkbox state.
\n\nMethod #1: The Classic — .prop() Method
\nThe modern and correct way. Since jQuery 1.6, for boolean attributes (checked, disabled, selected), you should use .prop().
// HTML\n<input type="checkbox" id="agree"> I agree to the terms\n\n// jQuery\nif ($('#agree').prop('checked')) {\n console.log('Checkbox is selected!');\n} else {\n console.log('Checkbox is not selected.');\n}\n\nWhy does this work? .prop() accesses the DOM element's property, which always returns true or false. It's fast, reliable, and without surprises.
Method #2: The :checked Selector (Concise)
\nWant it even shorter? Use the :checked pseudo-selector. It checks if the element is selected right in the selector.
if ($('#agree:checked').length) {\n console.log('Selected!');\n} else {\n console.log('Not selected.');\n}\n\nOr even more elegantly:
\nif ($('#agree').is(':checked')) {\n console.log('Selected!');\n}\n\nThe .is() method returns true if at least one element from the selection matches the selector. Clean and readable.
Method #3: The Old .attr() — Watch Out for Pitfalls!
\nSometimes in old tutorials you'll come across code like this:
\n\n// BAD example\nif ($('#agree').attr('checked')) {\n console.log('Selected');\n}\n\nDon't do this! .attr() reads the HTML attribute, which does not change after a user click. It will return undefined or the string "checked" only if the attribute is initially written in the HTML. It does not track the dynamic state. Looking for a bug?
Live Example: A Form with Multiple Checkboxes
\nLet's put it all together. Imagine we have a list of hobbies, and we want to know which ones the user selected.
\n\n<h3>Choose your hobbies:</h3>\n<label><input type="checkbox" class="hobby" value="coding"> Programming</label>\n<label><input type="checkbox" class="hobby" value="gaming"> Gaming</label>\n<label><input type="checkbox" class="hobby" value="reading"> Reading</label>\n<button id="showHobbies">Show Selected</button>\n\n<script>\n$('#showHobbies').on('click', function() {\n let selected = [];\n $('.hobby:checked').each(function() {\n selected.push($(this).val());\n });\n if (selected.length) {\n alert('You selected: ' + selected.join(', '));\n } else {\n alert('Nothing selected.');\n }\n});\n</script>\n\nNotice: we use .hobby:checked — this is a selector that immediately filters only the