{"id":752,"date":"2012-11-08T20:14:03","date_gmt":"2012-11-08T15:14:03","guid":{"rendered":"http:\/\/www.esearing.com\/v2010\/?p=752"},"modified":"2012-11-08T20:16:44","modified_gmt":"2012-11-08T15:16:44","slug":"javascript-checkboxes-are-checked","status":"publish","type":"post","link":"https:\/\/www.esearing.com\/v2010\/2012\/11\/javascript-checkboxes-are-checked\/","title":{"rendered":"Javascript &#8211; checkboxes are checked?"},"content":{"rendered":"<p>I ran into a bit of trouble when my javascript was updated on the coldfusion server. The method I had used for years to evaluate whether checkboxes were checked was no longer valid. Here is the new solution for 2012 forward. The function below would be triggered by the submit button on the form to validate that there are checboxes checked. Works with many check boxes name=&#8217;cbox&#8217;, or single checkbox with name=&#8217;cbox&#8217;.  One other note, make sure your checkbox has a value to pass to the next page. Field values of Null doesn&#8217;t play nice for some reason.<\/p>\n<p><code><br \/>\nfunction myFunctionName(){<br \/>\n\/\/is anything selected?<br \/>\n\tvar maxc = document.getElementsByName('cbox').length ;<br \/>\n\tvar inputlist = document.getElementsByName('cbox');<\/p>\n<p>\tif (maxc == 0){<br \/>\n\t\talert(\"You must select at least 1 checkbox.\");<br \/>\n\t\treturn false;<br \/>\n\t}<\/p>\n<p>\tif (maxc > 0){<br \/>\n\t\tfor (i=0; i < maxc; i++){\n\t\t\tif (inputlist[i].checked == true){\n\t\t\t\twith (document.updform)\n\t\t\t\t\t{\n\t\t\t\t\taction=\"yourpage.cfm\";\n\t\t\t\t\tmethod=\"post\";\n\t\t\t\t\tsubmit();\n\t\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\t\t\n\t\n}\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to Evaluate if Check Boxes are checked when submitting a form. Throws an error if none are checked.  <a href=\"https:\/\/www.esearing.com\/v2010\/2012\/11\/javascript-checkboxes-are-checked\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,21,24],"tags":[],"class_list":["post-752","post","type-post","status-publish","format-standard","hentry","category-javascript","category-problems-fixes","category-snippets-quick-code-tips-for-single-elements-attributes-or-details"],"_links":{"self":[{"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/posts\/752","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/comments?post=752"}],"version-history":[{"count":3,"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/posts\/752\/revisions"}],"predecessor-version":[{"id":759,"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/posts\/752\/revisions\/759"}],"wp:attachment":[{"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/media?parent=752"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/categories?post=752"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/tags?post=752"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}