{"id":4165,"date":"2021-07-02T15:39:31","date_gmt":"2021-07-02T15:39:31","guid":{"rendered":"https:\/\/www.leadshook.com\/help\/?p=4165"},"modified":"2024-02-14T11:40:11","modified_gmt":"2024-02-14T11:40:11","slug":"limit-the-number-of-checkbox-choices","status":"publish","type":"post","link":"https:\/\/www.leadshook.com\/help\/limit-the-number-of-checkbox-choices\/","title":{"rendered":"Limit the number of checkbox choices"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Sometimes you might need to set a maximum number of available choices when using checkboxes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example if you had eight choices and you only wanted a max of three of them accepted!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For this purpose we will use some custom JavaScript to count how many checks are allowed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Using the custom page node, the script looks like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;h1&gt;Select Any Two Checkboxs&lt;\/h1&gt;\n&lt;div id=\"checkboxgroup\"&gt;\n  &lt;input type=\"checkbox\" value=\"CheckBox 1\" name=\"cbx1\"&gt; Aubrey\n\t&lt;input type=\"checkbox\" value=\"CheckBox 2\" name=\"cbx2\"&gt; Laura\n\t&lt;input type=\"checkbox\" value=\"CheckBox 3\" name=\"cbx3\"&gt; Byron\n  \n  &lt;!-- Note #1 Add &lt;input type=\"checkbox\"... lines as necessary --&gt;\n  \n&lt;\/div&gt;\n\n&lt;script&gt;\nfunction onlyOneCheckBox() {\n\tvar checkboxgroup = document.getElementById('checkboxgroup').getElementsByTagName(\"input\");\n\t\n    \/\/Note #2 Change max limit here as necessary\n    var limit = 2;\n  \n\tfor (var i = 0; i &lt; checkboxgroup.length; i++) {\n\t\tcheckboxgroup&#91;i].onclick = function() {\n\t\t\tvar checkedcount = 0;\n\t\t\t\tfor (var i = 0; i &lt; checkboxgroup.length; i++) {\n\t\t\t\tcheckedcount += (checkboxgroup&#91;i].checked) ? 1 : 0;\n\t\t\t}\n\t\t\tif (checkedcount &gt; limit) {\n\t\t\t\tconsole.log(\"You can select maximum of \" + limit + \" checkbox.\");\n\t\t\t\talert(\"You can select maximum of \" + limit + \" checkbox.\");\n\t\t\t\tthis.checked = false;\n\t\t\t}\n\t\t}\n\t}\n}\n&lt;\/script&gt;\n\n&lt;script type=\"text\/javascript\"&gt;\n\tonlyOneCheckBox()\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Simply copy\/paste this code into a custom page node as is.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">And modify it as necessary. There are 2 comments to guide you. Making the process completely simple.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s a working demo: <a href=\"https:\/\/test2.leadshook.io\/survey\/ipos2BvlCurHDMqlx9aMR5vAGxZ2MBbcreCHZ6jv\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/test2.leadshook.io\/survey\/ipos2BvlCurHDMqlx9aMR5vAGxZ2MBbcreCHZ6jv<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">By using custom JavaScript, you can limit the number of checkboxes a user can select, providing additional control over user inputs in your decision trees.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction: Sometimes you might need to set a maximum number of available choices when using checkboxes. For example if you had eight choices and you only wanted a max of three of them accepted! For this purpose we will use some custom JavaScript to count how many checks are allowed. Using the custom page node, [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[620],"tags":[205,839,36,840,838,837,567],"class_list":["post-4165","post","type-post","status-publish","format-standard","hentry","category-scripts","tag-checkbox","tag-choices","tag-javascript","tag-limit","tag-max","tag-maximum","tag-script"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Limit the number of checkbox choices - LeadsHook Knowledge Base<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.leadshook.com\/help\/limit-the-number-of-checkbox-choices\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Limit the number of checkbox choices - LeadsHook Knowledge Base\" \/>\n<meta property=\"og:description\" content=\"Introduction: Sometimes you might need to set a maximum number of available choices when using checkboxes. For example if you had eight choices and you only wanted a max of three of them accepted! For this purpose we will use some custom JavaScript to count how many checks are allowed. Using the custom page node, [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.leadshook.com\/help\/limit-the-number-of-checkbox-choices\/\" \/>\n<meta property=\"og:site_name\" content=\"LeadsHook Knowledge Base\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/leadshook\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-02T15:39:31+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-02-14T11:40:11+00:00\" \/>\n<meta name=\"author\" content=\"Nik T\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@leadshook\" \/>\n<meta name=\"twitter:site\" content=\"@leadshook\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Nik T\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/limit-the-number-of-checkbox-choices\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/limit-the-number-of-checkbox-choices\\\/\"},\"author\":{\"name\":\"Nik T\",\"@id\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/#\\\/schema\\\/person\\\/4bfe37d814563cc729828b7055313f4d\"},\"headline\":\"Limit the number of checkbox choices\",\"datePublished\":\"2021-07-02T15:39:31+00:00\",\"dateModified\":\"2024-02-14T11:40:11+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/limit-the-number-of-checkbox-choices\\\/\"},\"wordCount\":135,\"publisher\":{\"@id\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/#organization\"},\"keywords\":[\"checkbox\",\"choices\",\"javascript\",\"limit\",\"max\",\"maximum\",\"script\"],\"articleSection\":[\"Scripts\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/limit-the-number-of-checkbox-choices\\\/\",\"url\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/limit-the-number-of-checkbox-choices\\\/\",\"name\":\"Limit the number of checkbox choices - LeadsHook Knowledge Base\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/#website\"},\"datePublished\":\"2021-07-02T15:39:31+00:00\",\"dateModified\":\"2024-02-14T11:40:11+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/limit-the-number-of-checkbox-choices\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.leadshook.com\\\/help\\\/limit-the-number-of-checkbox-choices\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/limit-the-number-of-checkbox-choices\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Limit the number of checkbox choices\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/#website\",\"url\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/\",\"name\":\"LeadsHook Knowledge Base\",\"description\":\"LeadsHook Training &amp; Technical Help\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/#organization\",\"name\":\"LeadsHook\",\"url\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/wp-content\\\/uploads\\\/2018\\\/12\\\/leadshook-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/wp-content\\\/uploads\\\/2018\\\/12\\\/leadshook-logo.png\",\"width\":350,\"height\":83,\"caption\":\"LeadsHook\"},\"image\":{\"@id\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/leadshook\\\/\",\"https:\\\/\\\/x.com\\\/leadshook\",\"https:\\\/\\\/www.linkedin.com\\\/showcase\\\/leadshook\\\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/#\\\/schema\\\/person\\\/4bfe37d814563cc729828b7055313f4d\",\"name\":\"Nik T\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/48d53e68db0b1db1cd5c54a750130fb2ee0c663306ff1bd434373f1a368ef7a0?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/48d53e68db0b1db1cd5c54a750130fb2ee0c663306ff1bd434373f1a368ef7a0?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/48d53e68db0b1db1cd5c54a750130fb2ee0c663306ff1bd434373f1a368ef7a0?s=96&d=mm&r=g\",\"caption\":\"Nik T\"},\"sameAs\":[\"http:\\\/\\\/www.LeadsHook.com\"],\"url\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/author\\\/nik\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Limit the number of checkbox choices - LeadsHook Knowledge Base","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.leadshook.com\/help\/limit-the-number-of-checkbox-choices\/","og_locale":"en_US","og_type":"article","og_title":"Limit the number of checkbox choices - LeadsHook Knowledge Base","og_description":"Introduction: Sometimes you might need to set a maximum number of available choices when using checkboxes. For example if you had eight choices and you only wanted a max of three of them accepted! For this purpose we will use some custom JavaScript to count how many checks are allowed. Using the custom page node, [&hellip;]","og_url":"https:\/\/www.leadshook.com\/help\/limit-the-number-of-checkbox-choices\/","og_site_name":"LeadsHook Knowledge Base","article_publisher":"https:\/\/www.facebook.com\/leadshook\/","article_published_time":"2021-07-02T15:39:31+00:00","article_modified_time":"2024-02-14T11:40:11+00:00","author":"Nik T","twitter_card":"summary_large_image","twitter_creator":"@leadshook","twitter_site":"@leadshook","twitter_misc":{"Written by":"Nik T","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.leadshook.com\/help\/limit-the-number-of-checkbox-choices\/#article","isPartOf":{"@id":"https:\/\/www.leadshook.com\/help\/limit-the-number-of-checkbox-choices\/"},"author":{"name":"Nik T","@id":"https:\/\/www.leadshook.com\/help\/#\/schema\/person\/4bfe37d814563cc729828b7055313f4d"},"headline":"Limit the number of checkbox choices","datePublished":"2021-07-02T15:39:31+00:00","dateModified":"2024-02-14T11:40:11+00:00","mainEntityOfPage":{"@id":"https:\/\/www.leadshook.com\/help\/limit-the-number-of-checkbox-choices\/"},"wordCount":135,"publisher":{"@id":"https:\/\/www.leadshook.com\/help\/#organization"},"keywords":["checkbox","choices","javascript","limit","max","maximum","script"],"articleSection":["Scripts"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.leadshook.com\/help\/limit-the-number-of-checkbox-choices\/","url":"https:\/\/www.leadshook.com\/help\/limit-the-number-of-checkbox-choices\/","name":"Limit the number of checkbox choices - LeadsHook Knowledge Base","isPartOf":{"@id":"https:\/\/www.leadshook.com\/help\/#website"},"datePublished":"2021-07-02T15:39:31+00:00","dateModified":"2024-02-14T11:40:11+00:00","breadcrumb":{"@id":"https:\/\/www.leadshook.com\/help\/limit-the-number-of-checkbox-choices\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.leadshook.com\/help\/limit-the-number-of-checkbox-choices\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.leadshook.com\/help\/limit-the-number-of-checkbox-choices\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.leadshook.com\/help\/"},{"@type":"ListItem","position":2,"name":"Limit the number of checkbox choices"}]},{"@type":"WebSite","@id":"https:\/\/www.leadshook.com\/help\/#website","url":"https:\/\/www.leadshook.com\/help\/","name":"LeadsHook Knowledge Base","description":"LeadsHook Training &amp; Technical Help","publisher":{"@id":"https:\/\/www.leadshook.com\/help\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.leadshook.com\/help\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.leadshook.com\/help\/#organization","name":"LeadsHook","url":"https:\/\/www.leadshook.com\/help\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.leadshook.com\/help\/#\/schema\/logo\/image\/","url":"https:\/\/www.leadshook.com\/help\/wp-content\/uploads\/2018\/12\/leadshook-logo.png","contentUrl":"https:\/\/www.leadshook.com\/help\/wp-content\/uploads\/2018\/12\/leadshook-logo.png","width":350,"height":83,"caption":"LeadsHook"},"image":{"@id":"https:\/\/www.leadshook.com\/help\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/leadshook\/","https:\/\/x.com\/leadshook","https:\/\/www.linkedin.com\/showcase\/leadshook\/"]},{"@type":"Person","@id":"https:\/\/www.leadshook.com\/help\/#\/schema\/person\/4bfe37d814563cc729828b7055313f4d","name":"Nik T","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/48d53e68db0b1db1cd5c54a750130fb2ee0c663306ff1bd434373f1a368ef7a0?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/48d53e68db0b1db1cd5c54a750130fb2ee0c663306ff1bd434373f1a368ef7a0?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/48d53e68db0b1db1cd5c54a750130fb2ee0c663306ff1bd434373f1a368ef7a0?s=96&d=mm&r=g","caption":"Nik T"},"sameAs":["http:\/\/www.LeadsHook.com"],"url":"https:\/\/www.leadshook.com\/help\/author\/nik\/"}]}},"_links":{"self":[{"href":"https:\/\/www.leadshook.com\/help\/wp-json\/wp\/v2\/posts\/4165","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.leadshook.com\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.leadshook.com\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.leadshook.com\/help\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.leadshook.com\/help\/wp-json\/wp\/v2\/comments?post=4165"}],"version-history":[{"count":2,"href":"https:\/\/www.leadshook.com\/help\/wp-json\/wp\/v2\/posts\/4165\/revisions"}],"predecessor-version":[{"id":6738,"href":"https:\/\/www.leadshook.com\/help\/wp-json\/wp\/v2\/posts\/4165\/revisions\/6738"}],"wp:attachment":[{"href":"https:\/\/www.leadshook.com\/help\/wp-json\/wp\/v2\/media?parent=4165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.leadshook.com\/help\/wp-json\/wp\/v2\/categories?post=4165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.leadshook.com\/help\/wp-json\/wp\/v2\/tags?post=4165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}