MikoLone
12-09-2002, 05:12 PM
I have this form:
<FORM NAME="Evaluation Form" METHOD="POST">
<input name="q19a" type="checkbox" id="q19a" value="Customer needs and demands">
<input name="q19b" type="checkbox" id="q19b" value="Customer needs and demands">
<input name="q19c" type="checkbox" id="q19c" value="Customer needs and demands">
<input name="q19d" type="checkbox" id="q19d" value="Customer needs and demands">
</form>
And I want to call a function when someone clicks the checkbox that will simply count how many are are checked
So I was thinking something like this
<input name="q19d" type="checkbox" id="q19d" value="Customer needs and demands" onClick=if(this.checked){counter(true);}else{counter(false);}>
I doubt the syntax will work. I need to know how to tell if it is being checked or unchecked.
Any help or suggestions will be greatly appreciated.
Thank you
MikoL:thumbsup:
beetle
12-09-2002, 05:57 PM
Actually, the checked property is exactly what you would use. If counter is your variable, then just increment/decrement it as needed.
<input name="q19d" type="checkbox" id="q19d" value="Customer needs and demands" onClick="counter += (this.checked) ? 1 : -1;" />
:D
A1ien51
12-09-2002, 05:57 PM
here is a script i scrounged up for a basic idea
<script>
Qs = new Array('a','b','c','d','e','f','g','h');
function CheckIt(X,Y){
NumChecked=0;
for(i=0;i<Y;i++){
if(document.A[X+Qs[i]].checked)NumChecked++;
}
alert(NumChecked)
}
</script>
<form name="A">
<input type="checkbox" name="q1a" onclick="CheckIt('q1',4)">1a
<input type="checkbox" name="q1b" onclick="CheckIt('q1',4)">1b
<input type="checkbox" name="q1c" onclick="CheckIt('q1',4)">1c
<input type="checkbox" name="q1d" onclick="CheckIt('q1',4)">1d
<BR>
<input type="checkbox" name="q2a" onclick="CheckIt('q2',3)">2a
<input type="checkbox" name="q2b" onclick="CheckIt('q2',3)">2b
<input type="checkbox" name="q2c" onclick="CheckIt('q2',3)">2c
</form>
Are you trying to limit a person to a certain number of selections?? Then here is a basic idea:
<script>
Qs = new Array('a','b','c','d','e','f','g','h');
function CheckIt(X,Y,Z,W){
NumChecked=0;
for(i=0;i<Z;i++){
if(document.A[X+Qs[i]].checked)NumChecked++;
}
if(NumChecked>Y){W.checked=false;W.blur();}
}
</script>
<form name="A">
Limited to 2 Selections:
<input type="checkbox" name="q1a" onclick="CheckIt('q1',2,4,this)">1a
<input type="checkbox" name="q1b" onclick="CheckIt('q1',2,4,this)">1b
<input type="checkbox" name="q1c" onclick="CheckIt('q1',2,4,this)">1c
<input type="checkbox" name="q1d" onclick="CheckIt('q1',2,4,this)">1d
<BR>
Limited to 1 Selection :
<input type="checkbox" name="q2a" onclick="CheckIt('q2',1,3,this)">2a
<input type="checkbox" name="q2b" onclick="CheckIt('q2',1,3,this)">2b
<input type="checkbox" name="q2c" onclick="CheckIt('q2',1,3,this)">2c
</form>
These scripts are a little more beefy then needed since I have the ability to change the number of check boxes for each group. You can get rid of that function by changing the for loops. There are other ways (prob. easier) to do it.
Hopes this helps you out
MikoLone
12-09-2002, 06:04 PM
Yep that did it. Thank you all for the replies and the code.
MikoLone:thumbsup:
requestcode
12-09-2002, 06:07 PM
Here is another way:
<html>
<head>
<title>Count if Checked</title>
<SCRIPT LANGUAGE="JavaScript">
var count=0
</SCRIPT>
</head>
<body>
<FORM NAME="myform">
<INPUT TYPE="checkbox" NAME="ck1" VALUE="ck1" onClick="if(this.checked){count++}else{count--}">
<INPUT TYPE="checkbox" NAME="ck2" VALUE="ck1" onClick="if(this.checked){count++}else{count--}">
<INPUT TYPE="checkbox" NAME="ck3" VALUE="ck1" onClick="if(this.checked){count++}else{count--}">
<INPUT TYPE="button" VALUE="Get Count" onClick="alert(count)">
</FORM>
</body>
</html>
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.