View Single Post
08-19-2008, 03:26 PM
#1
JohnK is offline JohnK
Status: Junior Member
Join date: Apr 2008
Location:
Expertise:
Software:
 
Posts: 44
iTrader: -1 / 0%
 

JohnK is on a distinguished road

  Old  Counting how many form elements have been selected

Well that's generally not so difficult and works something like this:

Code:
function countCheckboxes() {
var total = 0;
var max = form.element.length;
for (var i=0; i<max; i++) {
if (eval("document.form.element[" + i + "].checked") == true) {
    total += 1;
   }
}
alert(total + " checkboxes have been selected.");
}
... and the countCheckboxes() function is then called onclick at the submit button.

But: this only works if more than one checkbox exists in the HTML source. The "length" object returns "undefined" if there's only one checkbox! And the counting function doesn't work anymore.

Had anybody ever to deal with this problem and knows about a solution or a workaround?