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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

check box help

Status
Not open for further replies.

lostnewbie

Programmer
Nov 6, 2002
5
US
I'm totally lost when i was looking at the faq on check box. I have 4 check boxes and 2 of them contain the right answer. I want an alert box to pop up if none of the boxes are check. Then I want to check if the correct boxes are check. Let's say that the question ask for bob's favorite colors and the option of red, blue, black, and green was given. The correct answer is red and blue. If the person check the box red and blue they get 1pt added to their score. If they check only red or only blue they get no pts. If they check all the boxs I want an alert box to pop up saying they got the wrong answer.

I was using conditions but I got confuse, please help me
 
lost...

here's how to test if a box has been checked:

eval("document.formname.checkbox_name.checked") == true

In your case, you have 4 boxes, let's name them box1, box2, box3, & box4. Let's put red in box 2 and blue in box 4. So,

if (eval("document.formname.box1.checked") == true && eval("document.formname.box2.checked") == true && eval("document.formname.box3.checked") == true && eval("document.formname.box4.checked") == true){
alert("Wrong Answer!");
document.formname.box1.focus();
return (false);}

if (eval("document.formname.box2.checked") == true && eval("document.formname.box3.checked") == true){
alert("Correct! You get 1 point!";
return (true)}

Hope this helps There's always a better way...
 
is there a way for me to no have to type out a if statement for all the possible combination of wrong answer?
for examlpe red and blue is right answer so alert box pop up saying that the person gets the right answer, but if the person choose green and black then that is the wrong answer and an alert box will pop up. Red and Green is also the wrong answer so an alert box will pop up.
What I'm trying to say is do I have to type an if statement for all the combination of wrong answers or is there a shorter way?
 
if (eval("document.formname.box1.checked") == true && eval("document.formname.box2.checked") == true && eval("document.formname.box3.checked") == true && eval("document.formname.box4.checked") == true){
alert("Wrong Answer!");
document.formname.box1.focus();
return (false);}
else if (eval("document.formname.box2.checked") == true && eval("document.formname.box3.checked") == true){
alert("Correct! You get 1 point!";
return (true)}
else {alert("Wrong Answer!");
document.formname.box1.focus();
return (false);}

This will alert if all boxes checked, if correct answers checked, or if any other combination is checked. There's always a better way...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top