{"id":5228,"date":"2022-02-04T01:00:00","date_gmt":"2022-02-04T00:00:00","guid":{"rendered":"https:\/\/alvarotrigo.com\/epa\/jquery-checkbox-checked\/"},"modified":"2024-02-08T00:37:31","modified_gmt":"2024-02-07T23:37:31","slug":"jquery-checkbox-checked","status":"publish","type":"post","link":"https:\/\/alvarotrigo.com\/blog\/jquery-checkbox-checked\/","title":{"rendered":"Check if checkbox is checked in jQuery [With Examples]"},"content":{"rendered":"<p><strong>We can check the status of a checkbox by using the <code>:checked<\/code> jQuery selector together with the jQuery function <code>is<\/code>. For example: <code>$('#el').is(':checked')<\/code>.<\/strong><\/p>\n<p>Like on many things in life, there are many ways of doing the same thing, and this case is no different. There are different ways we can check if the checkbox is checked or not, then it&#8217;s up to you to choose the one that fits your use case better.<\/p>\n<p>\n\t\t\t<a href=\"https:\/\/codepen.io\/alvarotrigo\/pen\/yLXxPmL\" target=\"_blank\" rel=\"noopener nofollow\">\n\t\t\t\t<video width=\"680\" height=\"382\" muted autoplay loop playsinline>\n\t\t\t\t<source src=\"https:\/\/cdn.jsdelivr.net\/gh\/alvarotrigo\/blog-assets\/videos\/jquery-checkbox-checked\/check-if-checkbox-is-checked-jquery.webm\" type=\"video\/webm\"\/>\n\t\t\t\t<source src=\"https:\/\/cdn.jsdelivr.net\/gh\/alvarotrigo\/blog-assets\/videos\/jquery-checkbox-checked\/check-if-checkbox-is-checked-jquery.mp4\" type=\"video\/mp4\" \/>\n\t\t\t\tYour browser does not support the video tag.\n\t\t\t\t<\/source><\/source><\/video>\n\t\t\t<\/a>\n\t\t<\/p>\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Actually, this same methods are exactly the same methods used to <a href=\"https:\/\/alvarotrigo.com\/blog\/jquery-radio-button-checked\/\">check when a radio button is checked using jQuery<\/a>.<\/p>\n<\/blockquote>\n<h2 class=\"wp-block-heading\" id=\"1.-checking-if-a-checkbox-is-checked-by-using-is\">1. Checking if a checkbox is checked by using <code>is<\/code><\/h2>\n<p>This is actually the way I would recommend for anyone using jQuery:<\/p>\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">if<\/span>( $(<span class=\"hljs-string\">'#el'<\/span>).is(<span class=\"hljs-string\">':checked'<\/span>) ){\n    alert(<span class=\"hljs-string\">\"Checkbox Is checked\"<\/span>);\n}\n<span class=\"hljs-keyword\">else<\/span>{\n    alert(<span class=\"hljs-string\">\"Checkbox Is not checked\"<\/span>);\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>So how does it work? The <a href=\"https:\/\/api.jquery.com\/is\/\" rel=\"nofollow noopener\" target=\"_blank\"><code>is<\/code> method<\/a> of jQuery provides us a way to match one or multiple elements against a given selector, jQuery object, or element. It returns <code>true<\/code> if at least one element matches. Nice uh?<\/p>\n<p>So here we are selecting our checkbox element with <code>$('#el')<\/code> and asking jQuery if it matches the <code>:checked<\/code> selector.<\/p>\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_yLXxPmL\" data-src=\"\/\/codepen.io\/anon\/embed\/yLXxPmL?height=450&amp;theme-id=1&amp;slug-hash=yLXxPmL&amp;default-tab=css,result\" height=\"450\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed yLXxPmL\" title=\"CodePen Embed yLXxPmL\" class=\"cp_embed_iframe lazyload\" style=\"width:100%;overflow:hidden\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" data-load-mode=\"1\">CodePen Embed Fallback<\/iframe><\/div>\n\n<h2 class=\"wp-block-heading\" id=\"2.-checking-if-a-checkbox-is-checked-by-using-prop\">2. Checking if a checkbox is checked by using <code>prop<\/code><\/h2>\n<p>jQuery provides a <a href=\"https:\/\/api.jquery.com\/prop\/\" rel=\"nofollow noopener\" target=\"_blank\"><code>prop<\/code> method<\/a> that we can use to get the property of the JavaScript element. Because <code>checked<\/code> is a JavaScript property of checkbox type of inputs with type=&#8221;checkbox&#8221;, we can do the following to retrieve it:<\/p>\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-comment\">\/\/ Geting the boolean state<\/span>\n$(<span class=\"hljs-string\">'#el'<\/span>).prop(<span class=\"hljs-string\">'checked'<\/span>);\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>And, as before, we can use the returned boolean in our conditions, assign it to a variable, display it, etc.<\/p>\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>If you want to beautify your checkboxes you can&#8217;t miss this <a href=\"https:\/\/alvarotrigo.com\/blog\/css-checkbox-styles\/\">collection of great CSS checkbox styles<\/a>.<\/p>\n<\/blockquote>\n<h2 class=\"wp-block-heading\" id=\"3.-checking-if-a-checkbox-is-checked-using-a-selector\">3. Checking if a checkbox is checked using a selector<\/h2>\n<p>Another less elegant way to examine the state of our element is by using the <a href=\"https:\/\/api.jquery.com\/checked-selector\/\" rel=\"nofollow noopener\" target=\"_blank\">jQuery <code>:checked<\/code> selector<\/a> directly on it and then seeing if jQuery retrieves any element. We use the <code>.length<\/code> method to see how many elements are getting returned.<\/p>\n<p>Then, our condition will be <code>true<\/code> as long as we have 1 or more elements returned by our selector. And because <code>0<\/code> evaluates to <code>false<\/code>, if we find 0 elements, the condition won&#8217;t pass.<\/p>\n<p>Here&#8217;s an example:<\/p>\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-comment\">\/\/ Using the selector directly to see if the selector <\/span>\n<span class=\"hljs-comment\">\/\/ returns any element<\/span>\n<span class=\"hljs-keyword\">if<\/span>( $(<span class=\"hljs-string\">'#el:checked'<\/span>).length) ) {\n    alert(<span class=\"hljs-string\">'Is checked!'<\/span>);\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<h2 class=\"wp-block-heading\" id=\"4.-checking-if-a-checkbox-is-checked-with-javascript\">4. Checking if a checkbox is checked with JavaScript<\/h2>\n<p>Because jQuery is just another layer on top of JavaScript and it provides us a way to access directly the JavaScript object, we can use pure JavaScript to examine the state.<\/p>\n<p>We can access the JavaScript object by using brackets <code>[0]<\/code> or by using <code>.get(0)<\/code>. Both do exactly the same thing.<\/p>\n<p>Then we just access the <code>checked<\/code> property directly. It will return a <em>boolean<\/em> value we can then use as before:<\/p>\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-comment\">\/\/ Using &#91;0]<\/span>\n<span class=\"hljs-keyword\">if<\/span>( $(<span class=\"hljs-string\">'#el'<\/span>)&#91;<span class=\"hljs-number\">0<\/span>].checked ){ \n    alert(<span class=\"hljs-string\">\"Checkbox is checked!\"<\/span>);\n}\n\n<span class=\"hljs-comment\">\/\/ Using get<\/span>\n<span class=\"hljs-keyword\">if<\/span>( $(<span class=\"hljs-string\">'#el'<\/span>).get(<span class=\"hljs-number\">0<\/span>).checked ){\n    alert(<span class=\"hljs-string\">\"Checkbox is checked!\"<\/span>);\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<h2 class=\"wp-block-heading\" id=\"don't-use-attr-to-check-the-checkbox-status\">Don&#8217;t use <code>attr<\/code> to check the checkbox status<\/h2>\n<p>Do not use the jQuery function <code>attr<\/code> to check for the current state of a checkbox. This is a common mistake some developers do and that will for sure lead to problems.<\/p>\n<p>The HTML checkbox element, which is basically an <code>input<\/code> with <code>type=\"checkbox\"<\/code>, can contain an inline attribute to determine if the checkbox will appear <em>checked<\/em> on page load or not.<\/p>\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-comment\">&lt;!-- Checked on page load --&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">input<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"checkbox\"<\/span> <span class=\"hljs-attr\">name<\/span>=<span class=\"hljs-string\">\"test\"<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"el\"<\/span> <span class=\"hljs-attr\">checked<\/span>&gt;<\/span>\n\n<span class=\"hljs-comment\">&lt;!-- Unchecked on page load --&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">input<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"checkbox\"<\/span> <span class=\"hljs-attr\">name<\/span>=<span class=\"hljs-string\">\"test\"<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"el\"<\/span>&gt;<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>We can access attributes using jQuery when using the <code>attr<\/code> function. But here&#8217;s the deal: the attribute will still be <code>checked<\/code> even when the user changed it afterward. The attribute doesn&#8217;t reflect the state of the checkbox. It only reflects the default value on page load.<\/p>\n<p>So doing the following is a big mistake you want to avoid at all cost:<\/p>\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-comment\">\/\/ DO NOT DO! <\/span>\n<span class=\"hljs-comment\">\/\/ This will fail when the state of the checbox changes!<\/span>\n<span class=\"hljs-keyword\">if<\/span>( $(<span class=\"hljs-string\">'#el'<\/span>).attr(<span class=\"hljs-string\">'checked'<\/span>) ){\n    <span class=\"hljs-built_in\">console<\/span>.log(<span class=\"hljs-string\">\"Is checked!\"<\/span>);\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n<p>No matter what way you choose to <strong>check if a checkbox is checked<\/strong> as long as it works for you. Just remember to avoid using the <code>attr<\/code> option as that will lead to unwanted scenarios.<\/p>\n<p>Now that you&#8217;ve learned how to master the jQuery methods to get the state of an HTML element, you can do the same for other HTML elements. Usually, each state attribute has its own JavaScript property that you can access in the same way.<\/p>\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Read the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTML\/Element\/input\/checkbox\" rel=\"nofollow noopener\" target=\"_blank\">Checkbox Input (MDN Web Docs)<\/a> if you want to learn more about the HTML checkbox element.<\/p>\n<\/blockquote>\n<p>If you are learning JavaScript check out <a href=\"https:\/\/alvarotrigo.com\/blog\/best-way-learn-javascript\/\">what&#8217;s the best way to learn JavaScript!<\/a><\/p>\n<h2 class=\"wp-block-heading\" id=\"related-articles\">Related articles<\/h2>\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/alvarotrigo.com\/blog\/disable-button-jquery\/\">Disable Button with jQuery (With Examples)<\/a><\/li>\n<li><a href=\"https:\/\/alvarotrigo.com\/blog\/replace-text-with-jquery\/\">Replace Text with jQuery (With Examples)<\/a><\/li>\n<li><a href=\"https:\/\/alvarotrigo.com\/blog\/javascript-alert\/\">JavaScript Alert (Examples and Usage)<\/a><\/li>\n<li><a href=\"https:\/\/alvarotrigo.com\/blog\/javascript-select-option\/\">Change Selected Option in JavaScript (With Examples)<\/a><\/li>\n<li><a href=\"https:\/\/alvarotrigo.com\/blog\/prevent-scroll-on-scrollable-element-js\/\">Prevent Scroll On Scrollable Elements<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>To check the status of a checkbox in jQuery we have to use the `is` function and pass the `:checked` selector as a parameter. Here we show 4 ways of checking if a checkbox is checked.<\/p>\n","protected":false},"author":1,"featured_media":5227,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[120],"tags":[10,18],"class_list":["post-5228","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-jquery","tag-javascript","tag-jquery"],"acf":[],"_links":{"self":[{"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/posts\/5228","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/comments?post=5228"}],"version-history":[{"count":3,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/posts\/5228\/revisions"}],"predecessor-version":[{"id":8843,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/posts\/5228\/revisions\/8843"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/media\/5227"}],"wp:attachment":[{"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/media?parent=5228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/categories?post=5228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/tags?post=5228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}