{"id":1107,"date":"2022-08-30T15:12:30","date_gmt":"2022-08-30T15:12:30","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/how-to-reset-radiobuttons-in-jquery-so-that-none-is-checked-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:12:30","modified_gmt":"2022-08-30T15:12:30","slug":"how-to-reset-radiobuttons-in-jquery-so-that-none-is-checked-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/how-to-reset-radiobuttons-in-jquery-so-that-none-is-checked-collection-of-common-programming-errors\/","title":{"rendered":"How to reset radiobuttons in jQuery so that none is checked-Collection of common programming errors"},"content":{"rendered":"<p>In versions of jQuery before 1.6 use:<\/p>\n<pre><code>$('input[name=\"correctAnswer\"]').attr('checked', false);\n<\/code><\/pre>\n<p>In versions of jQuery after 1.6 you should use:<\/p>\n<pre><code>$('input[name=\"correctAnswer\"]').prop('checked', false);\n<\/code><\/pre>\n<p>but if you are using 1.6.1+ you can use the first form (see note 2 below).<\/p>\n<p><strong>Note 1:<\/strong> it is important that the second argument be <strong>false<\/strong> and not <strong>&#8220;false&#8221;<\/strong> since <strong>&#8220;false&#8221;<\/strong> is not a falsy value. i.e.<\/p>\n<pre><code>if (\"false\") {\n    alert(\"Truthy value. You will see an alert\");\n}\n<\/code><\/pre>\n<p><strong>Note 2:<\/strong> As of jQuery 1.6.0, there are now two similar methods, <code>.attr<\/code> and <code>.prop<\/code> that do two related but slightly different things. If in this particular case, the advice provide above works if you use 1.6.1+. The above will not work with 1.6.0, if you are using 1.6.0, you should upgrade. If you want the details, keep reading.<\/p>\n<p><strong>Details:<\/strong> When working with straight HTML DOM elements, there are properties attached to the DOM element (<code>checked<\/code>, <code>type<\/code>, <code>value<\/code>, etc) which provide an interface to the running state of the HTML page. There is also the <code>.getAttribute<\/code>\/<code>.setAttribute<\/code> interface which provides access to the HTML Attribute values as provided in the HTML. Before 1.6 jQuery blurred the distinction by providing one method, <code>.attr<\/code>, to access both types of values. jQuery 1.6+ provides two methods, <code>.attr<\/code> and <code>.prop<\/code> to get distinguish between these situations.<\/p>\n<p><code>.prop<\/code> allows you to set a property on a DOM element, while <code>.attr<\/code> allows you to set an HTML attribute value. If you are working with plain DOM and set the checked property, <code>elem.checked<\/code>, to <code>true<\/code> or <code>false<\/code> you change the running value (what the user sees) and the value returned tracks the on page state. <code>elem.getAttribute('checked')<\/code> however only returns the initial state (and returns <code>'checked'<\/code> or <code>undefined<\/code> depending on the initial state from the HTML). In 1.6.1+ using <code>.attr('checked', false)<\/code> does both <code>elem.removeAttribute('checked')<\/code> and <code>elem.checked = false<\/code> since the change caused a lot of backwards compatibility issues and it can&#8217;t really tell if you wanted to set the HTML attribute or the DOM property. See more information in the documentation for .prop.<\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:28:36. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>In versions of jQuery before 1.6 use: $(&#8216;input[name=&#8221;correctAnswer&#8221;]&#8217;).attr(&#8216;checked&#8217;, false); In versions of jQuery after 1.6 you should use: $(&#8216;input[name=&#8221;correctAnswer&#8221;]&#8217;).prop(&#8216;checked&#8217;, false); but if you are using 1.6.1+ you can use the first form (see note 2 below). Note 1: it is important that the second argument be false and not &#8220;false&#8221; since &#8220;false&#8221; is not a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1107","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1107","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1107"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1107\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}