{"id":1654,"date":"2017-06-19T16:45:59","date_gmt":"2017-06-19T11:15:59","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=1654"},"modified":"2026-09-13T16:25:00","modified_gmt":"2026-09-13T10:55:00","slug":"check-uncheck-checkbox-using-jquery","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/check-uncheck-checkbox-using-jquery\/","title":{"rendered":"How to Check or Uncheck a Checkbox with jQuery"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Checkboxes are a common form element, but detecting whether one is checked or unchecked in real time \u2014 and reacting to that change \u2014 trips up a lot of beginners working with jQuery. This guide shows how to check and uncheck a checkbox using jQuery, using the <code>.prop()<\/code> method and a simple click event to detect the checkbox&#8217;s current state.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Implementation<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">I. Prerequisites<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A web page you can add HTML and JavaScript to<\/li>\n\n\n\n<li>Basic familiarity with HTML forms<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">II. How This Works<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The browser fires a <strong>click event<\/strong> whenever the checkbox is clicked<\/li>\n\n\n\n<li>jQuery&#8217;s <strong>event handler<\/strong> catches that click and checks the checkbox&#8217;s current state<\/li>\n\n\n\n<li><strong><code>.prop('checked')<\/code><\/strong> returns <code>true<\/code> or <code>false<\/code> depending on whether the box is checked<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2017\/06\/checkbox_click_architecture-scaled.png\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"331\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2017\/06\/checkbox_click_architecture-1024x331.png\" alt=\"\" class=\"wp-image-11736\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2017\/06\/checkbox_click_architecture-1024x331.png 1024w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2017\/06\/checkbox_click_architecture-300x97.png 300w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2017\/06\/checkbox_click_architecture-768x248.png 768w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2017\/06\/checkbox_click_architecture-1536x497.png 1536w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2017\/06\/checkbox_click_architecture-2048x663.png 2048w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">III. Include the jQuery Library<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Before writing any jQuery code, you need to include the library itself. Add this inside your page&#8217;s <code>&lt;head&gt;<\/code> section:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;script src=\"https:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/3.7.1\/jquery.min.js\">&lt;\/script>\n<\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> The original CDN link for jQuery 1.8.1 is outdated and served over an insecure <code>http:\/\/<\/code> connection. The version above uses the current jQuery CDN over HTTPS \u2014 always prefer a recent, secure version unless you have a specific compatibility reason not to.<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">IV. Build the HTML Page<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s the complete, working example:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;!DOCTYPE html>\n&lt;html>\n&lt;head>\n  &lt;title>Check\/Uncheck Checkbox Example&lt;\/title>\n  &lt;script src=\"https:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/3.7.1\/jquery.min.js\">&lt;\/script>\n&lt;\/head>\n&lt;body>\n\n  &lt;input type=\"checkbox\" class=\"test\" value=\"\">\n\n  &lt;script>\n    $(document).on('click', '.test', function () {\n      if ($(this).prop('checked') === true) {\n        alert('Checked: true');\n      } else {\n        alert('Checked: false');\n      }\n    });\n  &lt;\/script>\n\n&lt;\/body>\n&lt;\/html>\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">V. What Changed From a Typical First Attempt<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A few small but important fixes make this version actually work:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>The jQuery include must use <code>&lt;script><\/code>, not <code>&lt;source><\/code>.<\/strong> The <code>&lt;source><\/code> tag is meant for <code>&lt;video><\/code>\/<code>&lt;audio><\/code>\/<code>&lt;picture><\/code> elements \u2014 browsers won&#8217;t execute JavaScript loaded through it, so the script would silently fail to run.<\/li>\n\n\n\n<li><strong>The click handler must live inside a <code>&lt;script><\/code> tag.<\/strong> Raw JavaScript placed directly in the HTML body (outside any <code>&lt;script><\/code> block) is treated as plain text by the browser and never executes.<\/li>\n\n\n\n<li><strong><code>$(this)<\/code> instead of <code>$('.test')<\/code> inside the handler.<\/strong> Using <code>$(this)<\/code> refers to the specific checkbox that was clicked, which matters if you ever have more than one checkbox sharing the <code>.test<\/code> class \u2014 <code>$('.test')<\/code> would always check the <em>first<\/em> matching element instead of the one the user actually clicked.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">VI. How It Works<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>$(document).on('click', '.test', function() {...})<\/code><\/strong> attaches a click event handler to any element with the class <code>test<\/code>, using event delegation so it also works on checkboxes added to the page dynamically after load.<\/li>\n\n\n\n<li><strong><code>$(this).prop('checked')<\/code><\/strong> returns <code>true<\/code> if the checkbox is currently checked, and <code>false<\/code> if it isn&#8217;t.<\/li>\n\n\n\n<li>The <code>if\/else<\/code> block simply checks that value and shows an alert reflecting the current state.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">VII. Conclusion<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Detecting a checkbox&#8217;s checked state in jQuery comes down to one method: <code>.prop('checked')<\/code>, checked inside a click handler. The example above fixes two easy-to-miss mistakes \u2014 using <code>&lt;source&gt;<\/code> instead of <code>&lt;script&gt;<\/code>, and leaving JavaScript outside a <code>&lt;script&gt;<\/code> tag \u2014 either of which would silently break the whole example. With those corrected, clicking the checkbox will reliably alert <code>true<\/code> when checked and <code>false<\/code> when unchecked.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently Asked Questions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Should I use <code>.prop()<\/code> or <code>.attr()<\/code> to check a checkbox&#8217;s state?<\/strong> Use <code>.prop()<\/code>. It reflects the checkbox&#8217;s current, live state (checked or not), while <code>.attr()<\/code> reflects the original HTML attribute and can give outdated results after the user interacts with the page.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>How do I programmatically check or uncheck a checkbox with jQuery, instead of just detecting clicks?<\/strong> Use <code>.prop('checked', true)<\/code> to check it, or <code>.prop('checked', false)<\/code> to uncheck it \u2014 for example: <code>$('.test').prop('checked', true);<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Why use event delegation (<code>$(document).on(...)<\/code>) instead of <code>$('.test').click(...)<\/code>?<\/strong> Event delegation ensures the handler still works for checkboxes added to the DOM after the page initially loads \u2014 a direct <code>.click()<\/code> binding only attaches to elements that already exist at the time the code runs.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Related Articles<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/set-static-ip-address-centos7-minimal-server\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Configure a Static IP Address on CentOS7<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/install-phpmyadmin-ubuntu\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Install phpMyAdmin on Ubuntu<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Talk to our experts.<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Looking for the right technology solution for your business? Our team of experts can help you with development, cloud, DevOps, design, and a wide range of other technology needs. Get in touch with our team <a href=\"https:\/\/pheonixsolutions.com\/contact\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Checkboxes are a common form element, but detecting whether one is checked or unchecked in real time \u2014 and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[350],"class_list":["post-1654","post","type-post","status-publish","format-standard","hentry","category-web-architecture","tag-javascriptjquery","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-qG","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/1654","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=1654"}],"version-history":[{"count":3,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/1654\/revisions"}],"predecessor-version":[{"id":11737,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/1654\/revisions\/11737"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=1654"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=1654"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=1654"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}