Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > counting checkboxes?

Reply
Thread Tools

counting checkboxes?

 
 
ASM
Guest
Posts: n/a
 
      07-19-2007
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[i].type=='checkbox') {
ch[ch.length] = f[i].value;
if(f[i].checked) c++;
}
}
alert('checked checboxes = '+c+'\nvalues = '+ch);
}

--
Stephane Moriaux et son (moins) vieux Mac
 
Reply With Quote
 
 
 
 
Evertjan.
Guest
Posts: n/a
 
      07-19-2007
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[i].checked)
n++


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
 
 
 
jmpinchot@gmail.com
Guest
Posts: n/a
 
      07-19-2007
On Jul 19, 4:45 pm, Tamer Higazi <(E-Mail Removed)> wrote:
> 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[i].checked;
// do something with the value
count++;
}
}
}

 
Reply With Quote
 
Tamer Higazi
Guest
Posts: n/a
 
      07-19-2007
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
 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      07-20-2007
On Jul 20, 7:32 am, "Evertjan." <(E-Mail Removed)> wrote:
> 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.


The value attribute is mandatory for radio buttons and checkboxes in
valid HTML:

<URL: http://www.w3.org/TR/html4/interact/...ef-value-INPUT
>



--
Rob


 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      07-20-2007
Kevin Scholl wrote on 20 jul 2007 in comp.lang.javascript:

> Evertjan. wrote:
>> 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.

>
> 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[i].checked)

>
> if (v[i].type == "checkbox" && v[i].checked)


Much better!


>
>> n++

>
>
>




--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
counting up instead of counting down edwardfredriks Javascript 6 09-07-2005 03:30 PM
Counting bits Steve VHDL 7 05-12-2004 05:42 AM
Re: tired of counting.... UDP socket bug Feroze [MSFT] ASP .Net 0 01-19-2004 02:42 AM
Re: tired of counting.... UDP socket bug Willy Denoyette [MVP] ASP .Net 0 01-13-2004 08:57 PM
Re: tired of counting.... UDP socket bug Ray Cassick \(Home\) ASP .Net 3 01-13-2004 06:53 PM



Advertisments