
February 12th, 2008, 11:18 AM
|
|
Contributing User
|
|
Join Date: Jan 2008
Posts: 123
Time spent in forums: 23 h 49 m 9 sec
Reputation Power: 1
|
|
Hey baioslaio, welcome to devshed! This is my first script (written in Javascript) so it may be a little raw but it does what I think you are looking for.
I am guessing that by everything going grey you mean the options (check boxes in my example) become disabled.
Code:
<head>
<script type="text/javascript" language="javascript">
function checkTicks() {
//cache document.form1.myboxes as it will be accessed repeatedly throughout the script
//will make the script more efficient
y = document.form1.myboxes;
//Initialise x and set it to 0 (will be used to determine
//if there are 3 checked boxes)
x=0;
//Access all the check boxes
for (i=0;i<y.length;i++) {
//If checked then add one to x
if (y[i].checked==true) {
x++;
}
}
//If three check boxes are checked then disable them
//but keep them checked
if (x==3){
for (i=0;i<y.length;i++) {
y[i].disabled=true;
//changes background to grey
document.bgColor="CCCCCC";
}
}
}
//Function to uncheck and re-enable all the boxes
//Used onload and with reset button
function enableBoxes() {
y = document.form1.myboxes;
for (var i=0;i<y.length;i++){
y[i].disabled=false;
y[i].checked=false;}
//resets background to default
document.bgColor="";
}
</script>
</head>
<body onload="enableBoxes()">
<div>
<form id="form1" name="form1"><h2>Question</h2>Asnwer 1
<input type="checkbox" name="myboxes" onClick="checkTicks()">
<br/>Answer 2
<input type="checkbox" name="myboxes" onClick="checkTicks()">
<br/>Answer 3
<input type="checkbox" name="myboxes" onClick="checkTicks()">
<br/>Answer 4
<input type="checkbox" name="myboxes" onClick="checkTicks()">
<br/>
<input type="button" value="reset" onClick="enableBoxes()">
</p>
</form>
</div>
I have tried to comment the code as much as possible for you (without making it look too clutteed) so you can see what is happening and adapt it as you want. The document.bgcolor=""; parts can be deleted without affecting the script, so feel free. I added it as I was not sure if you wanted the background to go grey or if you meant disable the check boxes.
I know that I have re-used some things, such as when trying to cache the document.form1.myboxes part of the code, I do not know why but when I tried to make it global by making it outside the functions it would not work, so more experienced developers feel free to solve that!
Hope this helps, if you have any questions about the code feel free to ask,
Mike
Last edited by acidedge2004 : February 12th, 2008 at 11:24 AM.
|