Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How many tickboxes?

Status
Not open for further replies.

DoctorGonzo

Programmer
Jan 23, 2004
48
GB
Hi guys,

Can anyone tell me how to get an integer of how many checkboxes (ELEMENTS?) are ticked in a field?

I want to limit the number of checkboxes that can be checked to 5, and can't find anything in forum (it telle me how to return the value, not the length?)

Many thanks,

Gonzo
 
you have to count them yourself. count the number in your group that have checked == true

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
 
Are there only one "set" of checkboxes?
You could do something like this:

Code:
var count = 0;
function countChecks(cb) {
    var f = document.forms['formname'];
    var els = f.elements;

    count = (cb.checked) ? (count+1) : (count-1);
    if (count > 5) {
        alert('Only 5 checks please!');
        count--;
        return false;
    }
}

And then call it like this:

Code:
<form name="formname">
<input type="checkbox" name="cb1" onclick="return countChecks(this);" />
<input type="checkbox" name="cb2" onclick="return countChecks(this);" />
<input type="checkbox" name="cb3" onclick="return countChecks(this);" />
<input type="checkbox" name="cb4" onclick="return countChecks(this);" />
<input type="checkbox" name="cb5" onclick="return countChecks(this);" />
<input type="checkbox" name="cb6" onclick="return countChecks(this);" />
<input type="checkbox" name="cb7" onclick="return countChecks(this);" />
<input type="checkbox" name="cb8" onclick="return countChecks(this);" />
</form>

*cLFlaVA
----------------------------
[tt]0101 is binary code for "supreme programmer of omnipotent power"[/tt] - adam0101
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Thabnks Flava, I've got it working in Noites and am now tweaking.

Can you tell me what the [var els = f.elements;] line is for as I can't see where the var is bneing referenced
(or does it just need to have it created to work?)

Many thanks!

Gonzo
 
What it does is first give you a variable that is the direct equivalent of the form, rather than having to reference document.form['formname'] every time. Then it assigns the whole list of elements (inputs, text areas, buttons, etc.) to its own variable so you don't have to refer to document.form['formname'].elements or f.elements.

He just mixed code he wrote from a couple experiments he tried with it:

var count = 0;
function countChecks(cb)
{
var f = document.forms['formname'];
var els = f.elements;

for (var ei=0;ei<elements.length;ei++)
{
if (els[ei].type=='checkbox')
{
if (els[ei].checked==true) count++;
}
if (count > 5)
{
alert('Only 5 checks please!');
count--;
return false;
}
}
return true;
}

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top