{"id":7672,"date":"2021-04-12T21:43:06","date_gmt":"2021-04-13T02:43:06","guid":{"rendered":"https:\/\/www.rushworth.us\/lisa\/?p=7672"},"modified":"2021-04-12T22:48:22","modified_gmt":"2021-04-13T03:48:22","slug":"changing-checkbox-value-when-unchecked","status":"publish","type":"post","link":"https:\/\/www.rushworth.us\/lisa\/?p=7672","title":{"rendered":"Changing checkbox value when (un)checked"},"content":{"rendered":"\n<p>This bit of code handles another rather esoteric scenario &#8212; I have a generic &#8220;go to this URL and download the resultant Excel file&#8221; JavaScript function. This is because I write a <em>lot<\/em> of reporting tools and didn&#8217;t want to write a lot of code for each new tool. The template is an input form with a submit button that calls the generic function. Params for the elements on the form from which values are read, the URL to call to generate the report, and the POST elements into which each corresponding form value is inserted gets stuffed. Works great for text inputs. Works fine for drop-downs. But the <em>value<\/em> of a checkbox is really a combination of the <em>potential<\/em> value (from the value tag) and the checked state. That is &#8212; my Button 1 has a potential value of 1, but if the box is checked or not is really important. <\/p>\n\n\n\n<p>Instead of attempting to determine the type of element in each form input so I can evaluate the checked condition, I decided to just <em>change<\/em> the value when the checkbox state is changed. Now Button 1 has a potential value of 0 when unchecked and a potential value of 1 when checked. I don&#8217;t need to <em>know<\/em> if the box is checked because the value answers that question. So passing along button1&#8217;s <em>value<\/em> to my URL lets the target site know if I want whatever Button 1 represents. (In this case, users are able to select from a list of seven data sources &#8212; smaller numbers of data sources reduce the query time but also fail to provide the most robust report). <\/p>\n\n\n\n<p>The JavaScript to handle changing the checkbox value when the checked state changes:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n$(&quot;#button1&quot;).change(function () {\n    if ($(&quot;#button1&quot;).is(&#039;:checked&#039;)) {\n        $(&quot;#button1&quot;).val(1);\n    }\n    else{\n        $(&quot;#button1&quot;).val(0);\n    }\n});\n\n$(&quot;#button2&quot;).change(function () {\n    if ($(&quot;#button2&quot;).is(&#039;:checked&#039;)) {\n        $(&quot;#button2&quot;).val(1);\n    }\n    else{\n        $(&quot;#button2&quot;).val(0);\n    }\n});\n\n<\/pre><\/div>\n\n\n<p>The HTML defining these two checkboxes:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;input type=&quot;checkbox&quot; id=&quot;button1&quot; name=&quot;button1&quot; value=&quot;1&quot; checked&gt;&lt;label for=&quot;ngmss&quot;&gt;Thing 1&lt;\/label&gt;\n&lt;input type=&quot;checkbox&quot; id=&quot;button2&quot; name=&quot;button2&quot; value=&quot;1&quot; checked&gt;&lt;label for=&quot;ngmss&quot;&gt;Thing 2&lt;\/label&gt;\n\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>This bit of code handles another rather esoteric scenario &#8212; I have a generic &#8220;go to this URL and download the resultant Excel file&#8221; JavaScript function. This is because I write a lot of reporting tools and didn&#8217;t want to write a lot of code for each new tool. The template is an input form &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33],"tags":[765,856,854,811],"class_list":["post-7672","post","type-post","status-publish","format-standard","hentry","category-coding","tag-html","tag-javascript","tag-jquery","tag-web-programming"],"_links":{"self":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/7672","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7672"}],"version-history":[{"count":2,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/7672\/revisions"}],"predecessor-version":[{"id":7674,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/7672\/revisions\/7674"}],"wp:attachment":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7672"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7672"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7672"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}