Javascript – checkboxes are checked?

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=’cbox’, or single checkbox with name=’cbox’. One other note, make sure your checkbox has a value to pass to the next page. Field values of Null doesn’t play nice for some reason.

function myFunctionName(){
//is anything selected?
var maxc = document.getElementsByName('cbox').length ;
var inputlist = document.getElementsByName('cbox');

if (maxc == 0){
alert("You must select at least 1 checkbox.");
return false;

if (maxc > 0){
for (i=0; i < maxc; i++){ if (inputlist[i].checked == true){ with (document.updform) { action="yourpage.cfm"; method="post"; submit(); } } } } }

Cup size   
Select size then click on coffee cup.
This entry was posted in Javascript, Problems & Fixes, Snippets. Bookmark the permalink.