I would like this to be hardcoded within the html page.
When I select a couple of checkbox and hit the OK button. A pop -up message box would appear with the results. I knew something is missing but still couldn’t figure out what’s the problem. :*(
<SCRIPT LANGUAGE="JavaScript">
<!--
function writeText1() {
//create for loop
for (var radIndex = 0; radIndex < document.form1.game.length-1; radIndex++) {
//define the form item as a variable for less writing later on
var formItem = document.form1.game[radIndex];
//create variable to store whether the user was correct or not
var ans;
//if the form item is checked and it's value tag is "tennis"
if (formItem.checked && formItem.value == "tennis") {
//set the ans variable to say it was correct
ans = "The answer is correct!";
//stop running this loop
break;
} else {
//else set the ans variable to say it was incorrect
ans = "Sorry! The answer is incorrect!";
}
};
//when for loop is finished, alert the value of the ans variable
alert(ans);
};
-->
</SCRIPT>
<FORM NAME="form1" onSubmit="writeText1()">
<INPUT TYPE="radio" NAME="game" VALUE="tennis">
Tennis <BR>
<INPUT TYPE="radio" NAME="game" VALUE="badminton">
Badminton <BR>
<INPUT TYPE="radio" NAME="game" VALUE="golf">
Golf <BR>
<INPUT TYPE="radio" NAME="game" VALUE="bowling">
Bowling <BR>
<INPUT TYPE="radio" NAME="game" VALUE="soccer">
Soccer <BR>
<INPUT TYPE="submit" NAME="Submit" VALUE="Submit">
</FORM>
And I used Radio Buttons because checkboxes allow you to select more than one answer, but radio buttons do not.
Okay, I have replace the radio button(s) with checkbox(es) it looks fine but I notice that if I select other sports besides tennis I’ll get the incorrect answer message.
How do I insert additional sports in this if statement?
if (formItem.checked && formItem.value == “tennis”)
Hmm, I have an idea on how to do it, but Javascript is sometimes tricky, right now I am working on something else, I will get to this as soon as I can.
In the meanwhile, experiment with ways to try and solve the problem, you might stumble onto something
Ok, Well I have come back, with an easier way to do it than a for loop (since that was what was throwing off the two items or more correct)
<SCRIPT LANGUAGE="JavaScript">
<!--
function writeText1() {
var ans1 = document.form1.game[0];
var ans2 = document.form1.game[2];
if (ans1.checked && ans2.checked){
alert("CORRECT ANSWER!");
} else {
alert("Nope sorry, wrong answer :**(");
}
};
-->
</SCRIPT>
This makes “tennis” and “golf” the correct answers.
The checkboxes are naturally stored in array with Javascript. Arrays begin counting at 0. So item one (in this case “tennis”) is at game[0], and item 3 (in this case is “golf”) is at game[2].
*Originally posted by snowflake *
**Question : How do u create the function that could check the value randomly instead? For example : The value ranging from [0],[2] & [4] **
Maybe it is just because I woke up, but I am not really understanding what you mean. Can you explain in more detail?
Let me post a direct question instead. I want each checkbox to be validated when I hit the Submit button. When I check ans1 to ans2 as true, do you think I should check the remaining ans as false too?
Another question,
If the scroll down appears on the web page and it keeps going to the top whenever I hit the Submit button for each questions. Is there a way to prevent it from going to the top?
No, there is no need to check for the others to be false. With the above script BOTH of those MUST be checked (returns the boolean value of true) for it to say it is true. All you need to check is if both of those answers are true, the false ones are just decoys so you don’t have to check them.
As for that window problem, I thought you could just void(0) out the button, apparently not, I will see if I can find a solution.