|
|
|
This example is going to require the user to answer three questions:
<input type="checkbox">I want to join the program
<input type="checkbox"">I want a free red hat
<input type="checkbox">I Love this web site
Give each checkbox a name corresponding to their label
<input type="checkbox" name="joinProgram">I want to join the program
<input type="checkbox" name="freeHat">I want a free red hat
<input type="checkbox" name="loveWebSite">I Love this web site
<input type="button">
Set the message on the button's face.
<input type="button"
value="Send">
Register the button's "onClick" event
<input type="button" value="Send"
onclick=" ">
Make the button's "onClick" event call the makeWindow() function
<input type="button" value="Send" onclick=
"checkRadio()">
This method first declares four local variables, joinProgram, freeHat , loveWebSite which are set to point to the checkboxes in the document and message that is set to a certain message depending on what checkboxes are checked.
After the checkbox references are set the script then goes through a six level deep nested if else sequence that tests five different checkbox check combinations.
The first condition checks if joinProgram is checked and loveWebSite is not checked. If this is the case message is set to "Why do you want to join the program when you do not love this web site?"
If the first condition was not satisfied the second condition checks if joinProgram is checked and freeHat is not checked. If this is the case message is set to "If your joining the program you can get a FREE red hat!"
If the second condition was not satisfied the third condition checks if loveWebSite is checked and freeHat is checked but joinProgram is not checked. If this is the case message is set "I Love this web site just like you and maybe the hat will look good on you But you can't have the hat if you do not join the program"
If the third condition was not satisfied the forth condition checks if the whole three are checked. If this is the case message is set to "You are my type of guy! Thank you for being interested in the our program that will keep you up all night with your free hat. You get all this and more because you love this web site and want everything we have to offer!"
If the forth condition was not satisfied the fifth condition checks if freeHat is checked but loveWebSite and joinProgram aren't checked. If this is the case message is set to ""No Hat for you because you do not love the web site and do not wanna join the program"
If the fifth condition was not satisfied which means no conditions were met so message is set to "Love is a wonderful thing but the hat and the program are much more wonderful!"
After processing all those nested if statements the message variable is alerted to the user with the alert method.
function checkAnswers(){
var joinProgram
var freeHat
var loveWebSite
var message
set up checkbox references
var joinProgram = document.myForm.joinProgram
var freeHat = document.myForm.freeHat
var loveWebSite = document.myForm.loveWebSite
Is joinProgram checked and loveWebSite not checked checked?
if(joinProgram.checked && !loveWebSite.checked){
message = "Why do you want to join the program when you hate this web site?"
Otherwise
}else{
if(joinProgram.checked && !freeHat.checked){
message = "If your joining the program you can get a FREE red hat!"
Otherwise
}else{
if(loveWebSite.checked && freeHat.checked && !joinProgram.checked){
message = "I Love this web site just like you and maybe the hat will look good on you\n"
+ "But you can't have the hat if you do not join the program"
Otherwise
}else{
if(loveWebSite.checked && freeHat.checked && joinProgram.checked){
message = "You are my type of guy!\n Thank you for being interested in the our program\n"
+ " that will keep you up all night while you ware the free hat we are going to give to you.\n"
+ "You get all this and more because you Love this web site and want everything we have to offer!"
Otherwise
}else{
if(freeHat.checked && !loveWebSite.checked && !joinProgram.checked){
message = "No Hat for you because you do not love the web site and do not wanna join the program"
Otherwise
}else{
message = "Love is a wonderful thing but the hat and the program are much more wonderful!"
}
}
}
}
alert the message
alert(message)
}
Run Code | View Code
See also: Radio button | Querying a text field's value | Select