counting checkboxes?

A

ASM

En réponse à Tamer Higazi qui nous a susurré, en date du : 20/07/07
1:45, le message sibyllin suivant :
Hi!
I have a form where I have embedded a table. Inside a column in each row
is a checkbox. I want:

A) to get the values of the checkboxes
B) to count the total amount of checkboxes (which are selected)

If somebody can give me an advise, I would thank him7her very much.

I think you have to do a loop on your checboxes

functions checks() {
var f = document.forms[0], c=0, ch=new Array();
for(var i=0; i<f.length; i++) {
if(f.type=='checkbox') {
ch[ch.length] = f.value;
if(f.checked) c++;
}
}
alert('checked checboxes = '+c+'\nvalues = '+ch);
}
 
E

Evertjan.

Tamer Higazi wrote on 20 jul 2007 in comp.lang.javascript:
Hi!
I have a form where I have embedded a table. Inside a column in each
row is a checkbox. I want:

A) to get the values of the checkboxes

Checkboxes have no value, methinks.
B) to count the total amount of checkboxes (which are selected)

If somebody can give me an advise, I would thank him7her very much.

As you probably expected, this can easily be done with Javascript.

The collection of inputs can be made available here:

var v = document.forms['myForm'].elements

Then you can look at each input and if the checked is true,
[it must be a checkbox, AND checkedm
increment the counter.

Try:

var v = document.forms['myForm'].elements
var n=0
for(i=0;i<v.length;i++)
if (v.checked)
n++
 
J

jmpinchot

Hi!
I have a form where I have embedded a table. Inside a column in each row
is a checkbox. I want:

A) to get the values of the checkboxes
B) to count the total amount of checkboxes (which are selected)

You can access all elements of a form by using the form.elements
collection. Then iterate through each and check that the current
element has the type "checkbox". The value of the checkbox element is
stored in the "checked" attribute. So try something like this:

var count = 0;
function foo() {
var form = document.getElementById('someForm');
var elements = form.elements;
for (var i = 0; i < elements.length; i++) {
if (elements.type == 'checkbox') {
var value = elements.checked;
// do something with the value
count++;
}
}
}
 
T

Tamer Higazi

Hi!
I have a form where I have embedded a table. Inside a column in each row
is a checkbox. I want:

A) to get the values of the checkboxes
B) to count the total amount of checkboxes (which are selected)

If somebody can give me an advise, I would thank him7her very much.


with kind regards


Tamer Higazi
 
E

Evertjan.

Kevin Scholl wrote on 20 jul 2007 in comp.lang.javascript:
Evertjan. said:
Tamer Higazi wrote on 20 jul 2007 in comp.lang.javascript:


Checkboxes have no value, methinks.

Eh? A checkbox indeed does have a value; it is what is passed upon
form submission if the checkbox is checked.

[input name="elemName" id="elemName" type="checkbox" value="Yes" /]

Mmmm... Yes.

B) to count the total amount of checkboxes (which are selected)

If somebody can give me an advise, I would thank him7her very much.

As you probably expected, this can easily be done with Javascript.

The collection of inputs can be made available here:

var v = document.forms['myForm'].elements

Then you can look at each input and if the checked is true,
[it must be a checkbox, AND checkedm

Why must it be a checkbox? Radio buttons also have a "checked"
attribute.
increment the counter.

Try:

var v = document.forms['myForm'].elements
var n=0
for(i=0;i<v.length;i++)
if (v.checked)


if (v.type == "checkbox" && v.checked)


Much better!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top