Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,472
There are 2212 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     Javascript :

Counting how many form elements have been selected

Thread title: Counting how many form elements have been selected
     
    Thread tools Search this thread Display Modes  
Prev Previous Post   Next Post Next
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?

     


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed