{"id":6808,"date":"2020-07-30T22:53:25","date_gmt":"2020-07-31T03:53:25","guid":{"rendered":"https:\/\/www.rushworth.us\/lisa\/?p=6808"},"modified":"2020-08-05T09:18:54","modified_gmt":"2020-08-05T14:18:54","slug":"html-checkboxes-to-add-and-remove-values-from-table","status":"publish","type":"post","link":"https:\/\/www.rushworth.us\/lisa\/?p=6808","title":{"rendered":"HTML Checkboxes To Add and Remove Values from Table"},"content":{"rendered":"\n<p>I am creating a web form where the user input <em>sometimes<\/em> cannot be resolved to a unique value. In those cases, I present the user a set of checkboxes with the options (yes, a radio button makes more sense because you can only select one. <strong><em>But<\/em><\/strong> I hate that radio buttons change selection when you hit an arrow key.). <\/p>\n\n\n\n<p>When a selection is made, I need to (1) deactivate the checkboxes for the other options when a checkbox in the group is selected and (2) add information to a data table that is used in subsequent activities. <\/p>\n\n\n\n<p>When a selection is cleared, I need to (1) activate the checkboxes within the group and (2) remove the right row from the data table. <\/p>\n\n\n\n<p>Below is the HTML code that achieves this. Now I just need to map this test page into the actual web code. There&#8217;s a post with the actual <a href=\"https:\/\/www.rushworth.us\/lisa\/?p=6813\" target=\"_blank\" rel=\"noreferrer noopener\">code I ended up using in production<\/a> too.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: xml; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\n&lt;html&gt;\n&lt;head&gt;&lt;title&gt;Adding And Removing From Table&lt;\/title&gt;&lt;\/head&gt;\n&lt;body&gt;\n\n&lt;div name=&quot;divCircuitClarifications&quot; id=&quot;divCircuitClarifications&quot;&gt;\n  &lt;h3&gt;My-Sample-Circuit-A&lt;\/h3&gt;\n    &lt;input type=&quot;checkbox&quot; value=&quot;123&quot; tableselector=&quot;SampleCircuitA&quot; name=&quot;SampleCircuitA&#x5B;]&quot; \/&gt;&lt;label&gt;123&lt;\/label&gt;\n    &lt;input type=&quot;checkbox&quot; value=&quot;234&quot; tableselector=&quot;SampleCircuitA&quot; name=&quot;SampleCircuitA&#x5B;]&quot; \/&gt;&lt;label&gt;234&lt;\/label&gt;\n    &lt;input type=&quot;checkbox&quot; value=&quot;345&quot; tableselector=&quot;SampleCircuitA&quot; name=&quot;SampleCircuitA&#x5B;]&quot; \/&gt;&lt;label&gt;345&lt;\/label&gt;\n&lt;P&gt;\n  &lt;h3&gt;My-Sample-Circuit-B&lt;\/h3&gt;\n    &lt;input type=&quot;checkbox&quot; value=&quot;abc&quot; tableselector=&quot;SampleCircuitB&quot; name=&quot;SampleCircuitB&#x5B;]&quot; \/&gt;&lt;label&gt;abc&lt;\/label&gt;\n    &lt;input type=&quot;checkbox&quot; value=&quot;bcd&quot; tableselector=&quot;SampleCircuitB&quot; name=&quot;SampleCircuitB&#x5B;]&quot; \/&gt;&lt;label&gt;bcd&lt;\/label&gt;\n    &lt;input type=&quot;checkbox&quot; value=&quot;cde&quot; tableselector=&quot;SampleCircuitB&quot; name=&quot;SampleCircuitB&#x5B;]&quot; \/&gt;&lt;label&gt;cde&lt;\/label&gt;\n&lt;P&gt;\n  &lt;h3&gt;My-Sample-Circuit-C&lt;\/h3&gt;\n    &lt;input type=&quot;checkbox&quot; value=&quot;Cabc&quot; tableselector=&quot;SampleCircuitC&quot; name=&quot;SampleCircuitC&#x5B;]&quot; \/&gt;&lt;label&gt;abc&lt;\/label&gt;\n    &lt;input type=&quot;checkbox&quot; value=&quot;Cbcd&quot; tableselector=&quot;SampleCircuitC&quot; name=&quot;SampleCircuitC&#x5B;]&quot; \/&gt;&lt;label&gt;bcd&lt;\/label&gt;\n    &lt;input type=&quot;checkbox&quot; value=&quot;Ccde&quot; tableselector=&quot;SampleCircuitC&quot; name=&quot;SampleCircuitC&#x5B;]&quot; \/&gt;&lt;label&gt;cde&lt;\/label&gt;\n&lt;P&gt;\n&lt;\/div&gt;\n\n&lt;div id=&quot;divResultTable&quot; name=&quot;divResultTable&quot;&gt;\n&lt;table border=&quot;1&quot; padding=&quot;1&quot; name=&quot;tableSetsToCreate&quot; id=&quot;tableSetsToCreate&quot;&gt;\n\t&lt;thead&gt;&lt;tr&gt;&lt;th&gt;ECCKT&lt;\/th&gt;&lt;th&gt;Circuit ID&lt;\/th&gt;&lt;\/tr&gt;&lt;\/thead&gt;\n\t&lt;tbody&gt;&lt;\/tbody&gt;\n&lt;\/table&gt;\n\n\n&lt;script src=&quot;https:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1.11.1\/jquery.min.js&quot;&gt;&lt;\/script&gt;\n\n&lt;script&gt;\n\t$(&quot;input:checkbox&quot;).on(&#039;click&#039;, function() {\n\t  \tvar $objCheckbox = $(this);\n\t  \tif ($objCheckbox.is(&quot;:checked&quot;)) {\t\t\t\/\/ When checked, deactivate other checkboxes and add to sets to create table\n\t    \t\tvar objCheckboxGroup = &quot;input:checkbox&#x5B;tableselector=&#039;&quot; + $objCheckbox.attr(&quot;tableselector&quot;) + &quot;&#039;]&quot;;\n\t\n\t    \t\t$(objCheckboxGroup).prop(&quot;disabled&quot;, true);\n\t    \t\t$objCheckbox.prop(&quot;disabled&quot;, false);\t\t\/\/ Allow checked box to be unchecked\n\n\t                var strTableRowString = &#039;&lt;tr&gt;&lt;td&gt;&#039; + $objCheckbox.attr(&quot;tableselector&quot;) + &#039;&lt;\/td&gt;&lt;td&gt;&#039; + $objCheckbox.val() + &#039;&lt;\/td&gt;\\n&#039;;\n\t                $(&#039;#tableSetsToCreate tbody&#039;).append(strTableRowString);\n\t  \t}\n\t\telse {\t\t\t\t\t\t\t\/\/ When unchecked, active checkboxes and remove from sets to create table\n\t    \t\tvar objCheckboxGroup = &quot;input:checkbox&#x5B;name=&#039;&quot; + $objCheckbox.attr(&quot;name&quot;) + &quot;&#039;]&quot;;\n\t    \t\t$(objCheckboxGroup).prop(&quot;disabled&quot;, false);\t\n\n\t\t\t$(&quot;tr:contains(&#039;&quot; + $objCheckbox.attr(&#039;tableselector&#039;) + &quot;&#039;)&quot;).each(function(){ $(this).remove();})\n  \t\t}\n\t});\n&lt;\/script&gt;\n&lt;\/body&gt;\n\n\n\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>I am creating a web form where the user input sometimes cannot be resolved to a unique value. In those cases, I present the user a set of checkboxes with the options (yes, a radio button makes more sense because you can only select one. But I hate that radio buttons change selection when you &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,1037,856,854,1036,811],"class_list":["post-6808","post","type-post","status-publish","format-standard","hentry","category-coding","tag-html","tag-html-forms","tag-javascript","tag-jquery","tag-web-development","tag-web-programming"],"_links":{"self":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/6808","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=6808"}],"version-history":[{"count":3,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/6808\/revisions"}],"predecessor-version":[{"id":6840,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/6808\/revisions\/6840"}],"wp:attachment":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6808"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6808"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6808"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}