{"id":5230,"date":"2022-03-24T01:00:00","date_gmt":"2022-03-24T00:00:00","guid":{"rendered":"https:\/\/alvarotrigo.com\/epa\/jquery-radio-button-checked\/"},"modified":"2024-02-08T00:38:52","modified_gmt":"2024-02-07T23:38:52","slug":"jquery-radio-button-checked","status":"publish","type":"post","link":"https:\/\/alvarotrigo.com\/blog\/jquery-radio-button-checked\/","title":{"rendered":"Check If Radio Button Is Checked In jQuery [With Examples]"},"content":{"rendered":"<p><strong>We can check the status of a radio button 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>It is exactly the same method we use to <a href=\"https:\/\/alvarotrigo.com\/blog\/jquery-checkbox-checked\/\">check when a checkbox is checked using jQuery<\/a>.<\/p>\n<p>\n\t\t\t<a href=\"https:\/\/codepen.io\/alvarotrigo\/pen\/NWXdBzz\" 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-radio-button-checked\/jquery-check-radio-button-checked.webm\" type=\"video\/webm\"\/>\n\t\t\t\t<source src=\"https:\/\/cdn.jsdelivr.net\/gh\/alvarotrigo\/blog-assets\/videos\/jquery-radio-button-checked\/jquery-check-radio-button-checked.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<h2 class=\"wp-block-heading\" id=\"1.-checking-if-a-radio-button-is-checked-by-using-is\">1. Checking If A Radio Button Is Checked By Using <code>is<\/code><\/h2>\n<p>This is the best and the most recommended way when 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\">'#radio'<\/span>).is(<span class=\"hljs-string\">':checked'<\/span>) ){\n    alert(<span class=\"hljs-string\">\"Radio Button Is checked!\"<\/span>);\n}\n<span class=\"hljs-keyword\">else<\/span>{\n    alert(<span class=\"hljs-string\">\"Radio Button 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>What we are doing here is making use of the <a href=\"https:\/\/api.jquery.com\/is\/\" rel=\"nofollow noopener\" target=\"_blank\">jQuery <code>is<\/code> method<\/a> to check if the provided element matches a given selector, a jQuery object, or an element. It returns <code>true<\/code> if there&#8217;s a match. Cool uh?<\/p>\n<p>So basically we do the selection of our element using <code>$('#radio')<\/code> (assuming our element uses <code>id=\"radio\"<\/code>) and then we ask jQuery if it matches the <code>:checked<\/code> selector. (Which will do if the radio is active)<\/p>\n<p>You can play with this simple example:<\/p>\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_NWXdBzz\" data-src=\"\/\/codepen.io\/anon\/embed\/NWXdBzz?height=450&amp;theme-id=1&amp;slug-hash=NWXdBzz&amp;default-tab=css,result\" height=\"450\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed NWXdBzz\" title=\"CodePen Embed NWXdBzz\" 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-radio-is-checked-by-using-prop\">2. Checking If A Radio Is Checked By Using <code>prop<\/code><\/h2>\n<p>There&#8217;s usually more than one way to do things and this case will be no different.<\/p>\n<p>Because jQuery also provides a way to check the properties of HTML elements, we can make use of the <a href=\"https:\/\/api.jquery.com\/prop\/\" rel=\"nofollow noopener\" target=\"_blank\"><code>prop<\/code> method<\/a> to check for the <code>checked<\/code> property:<\/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 state property which will be a boolean<\/span>\n$(<span class=\"hljs-string\">'#radio'<\/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\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_LYexBwx\" data-src=\"\/\/codepen.io\/anon\/embed\/LYexBwx?height=450&amp;theme-id=1&amp;slug-hash=LYexBwx&amp;default-tab=css,result\" height=\"450\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed LYexBwx\" title=\"CodePen Embed LYexBwx\" 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=\"3.-checking-if-a-radio-button-is-checked-using-a-selector\">3. Checking If A Radio Button Is Checked Using A Selector<\/h2>\n<p>Here&#8217;s another less recommended way to check the status of a radio button.<\/p>\n<p>It consists of directly using a selector with the <code>:checked<\/code> property to try to find the active radio button on the DOM. If we don&#8217;t find it, that means the element wasn&#8217;t active. And if we do find the radio button matching that selector, we can be certain that the radio button is checked.<\/p>\n<p>In order to find out if we found such an element or not, we make use of the <code>.length<\/code> method to see how many elements are getting returned.<\/p>\n<p>If the element is found, the <code>length<\/code> function will return 1 (or the number of matched elements) and 0 otherwise.<\/p>\n<p>This means we can use it directly in an <code>if<\/code> condition as 0 will be treated as <code>false<\/code> and anything above 0 like a <code>true<\/code> value.<\/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\">'#radio:checked'<\/span>).length) ) {\n    alert(<span class=\"hljs-string\">'Radio Button 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<p>Here&#8217;s a codepen you can play with:<\/p>\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_PoEWBgR\" data-src=\"\/\/codepen.io\/anon\/embed\/PoEWBgR?height=450&amp;theme-id=1&amp;slug-hash=PoEWBgR&amp;default-tab=css,result\" height=\"450\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed PoEWBgR\" title=\"CodePen Embed PoEWBgR\" 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=\"4.-checking-if-a-radio-button-is-checked-with-vanilla-javascript\">4. Checking If A Radio Button Is checked with Vanilla JavaScript<\/h2>\n<p>jQuery is just a layer on top of JavaScript, which means that anything jQuery can do can also be done in pure JavaScript in one way or another.<\/p>\n<p>If you are using jQuery and want to have access to the JavaScript object for the selected DOM element, all you have to do is select the first element in the jQuery object. We can do so by using <code>[0]<\/code> or <code>.get(0)<\/code>.<\/p>\n<p>Then we just access the <code>checked<\/code> property directly and it will return a <em>boolean<\/em> value we can then use as before.<\/p>\n<p>However, if you really want to get rid of jQuery, you can select the element directly by using JavaScript by using <code>documet.querySelector<\/code> if it&#8217;s a single element.<\/p>\n<p>Here are all the combinations, being the last the only one that is pure JavaScript.<\/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\">'#radio'<\/span>)&#91;<span class=\"hljs-number\">0<\/span>].checked ){ \n    alert(<span class=\"hljs-string\">\"Radio Button is checked!\"<\/span>);\n}\n\n<span class=\"hljs-comment\">\/\/ Using .get()<\/span>\n<span class=\"hljs-keyword\">if<\/span>( $(<span class=\"hljs-string\">'#radio'<\/span>).get(<span class=\"hljs-number\">0<\/span>).checked ){\n    alert(<span class=\"hljs-string\">\"Radio Button is checked!\"<\/span>);\n}\n\n<span class=\"hljs-comment\">\/\/ Using Vanilla JavaScript without jQuery<\/span>\n<span class=\"hljs-keyword\">if<\/span>( <span class=\"hljs-built_in\">document<\/span>.querySelector(<span class=\"hljs-string\">'#radio'<\/span>).checked ){\n    alert(<span class=\"hljs-string\">\"Radio Button 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-radio-button-status\">Don&#8217;t Use <code>attr<\/code> To Check The Radio Button Status<\/h2>\n<p>The HTML radio element, which is basically an <code>input<\/code> with <code>type=\"radio\"<\/code>, can contain an inline attribute to determine if the radio button 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\">\"radio\"<\/span> <span class=\"hljs-attr\">name<\/span>=<span class=\"hljs-string\">\"test\"<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"radio\"<\/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\">\"radio\"<\/span> <span class=\"hljs-attr\">name<\/span>=<span class=\"hljs-string\">\"test\"<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"radio2\"<\/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>jQuery provides another function called <code>attr<\/code> that allows us to get the attribute of a given element.<\/p>\n<p>Some developers make use of this method to get the value without realizing that this way we are only getting the initial value of the radio button.<\/p>\n<p>Any changes on its status won&#8217;t get reflected on this HTML attribute and therefore using <code>attr<\/code> to get its state will return incorrect values if the element changes its state dynamically through user interaction or programmatically.<\/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 radio changes!<\/span>\n<span class=\"hljs-keyword\">if<\/span>( $(<span class=\"hljs-string\">'#radio'<\/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-on-radio-buttons-status\">Conclusion On Radio Buttons Status<\/h2>\n<p>Choose the way you find more appropriate to <strong>check if a radio button is checked<\/strong>. Just make sure to not use the <code>attr<\/code> option as that will very likely lead to unwanted scenarios. (And if you use it, at least make sure to know why and the possible consequences)<\/p>\n<p>Now you&#8217;ve learned the basic methods to check the state of DOM elements. You can apply the same knowledge in order to <a href=\"https:\/\/alvarotrigo.com\/blog\/disable-button-jquery\/\">disable buttons in jQuery<\/a>, check the status of checkboxes, disable input texts, textareas, etc.<\/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\/radio\" rel=\"nofollow noopener\" target=\"_blank\">Radio Buttoon Input (MDN Web Docs)<\/a> if you want to learn more about the HTML radio 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\/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\/contact-page-examples\/\">Best Contact Page Examples<\/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\/css-checkbox-styles\/\">Beautiful CSS styles for your checkboxes<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>To check the status of a radio button 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 radio element is checked.<\/p>\n","protected":false},"author":1,"featured_media":5229,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[120],"tags":[10,18],"class_list":["post-5230","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\/5230","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=5230"}],"version-history":[{"count":3,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/posts\/5230\/revisions"}],"predecessor-version":[{"id":8844,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/posts\/5230\/revisions\/8844"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/media\/5229"}],"wp:attachment":[{"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/media?parent=5230"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/categories?post=5230"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/tags?post=5230"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}