View Full Version : null value = correct answer in checkbox javascript quiz ?

07-04-2002, 09:28 PM
Hi, - Terribly sorry to bother you sharkies, but this has ruined my sleep for a week - I am NOT a pro javascripter, merely a newbie trying to finish a quiz based on a script I found - various questions with checkboxes - Now the problem - For some of the questions the correct answer is UNCHECKED (null) - This must be SO basic but I cant find anything anywhere ... The correct answers are placed in an array on a result page - eg.

correctAnswers = new Array('1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1');

I hoped to be able to retrieve/confirm the value of the unchecked checkbox by inserting 'null' - but that didnt work (neither did false or 0 with/without brackets) - I know how to set values of form elements BUT the script has named each checkbox numerically to build the array - (yes I know thats bad coding Latin) - Here is a link to the quiz :


Yup, - thats danish !!!


Hope you can help me - would be much appreciated I tell you !

Best Theo Denmark

07-05-2002, 01:37 AM

couldn't you elaborate a bit more or post some snippets of code? It is not clear which the problem might be (nor what the array is for): with a few more infos we can be of some help probably.

Dave is right when he says you maybe meant to use radio boxes:

<input type="radio" name="group1" value="correct">
<input type="radio" name="group1" value="mistaken">
<input type="radio" name="group1" value="mistaken">

<input type="radio" name="group2" value="correct">
<input type="radio" name="group2" value="mistaken">
<input type="radio" name="group2" value="mistaken">

nothe each set (whichever amount) of radio boxes is insulated from the other elements by being assigned a shared group mame: group1, group2...
To access single elements:



To see if one is checked:

returns true if checked, false if not.

You can use loops:

for(var i=0; i<document.FORMNAME.group1.length;i++){
/*stuff for checked. Note the index is the loop variable i (or whatever letter you choose)*/
if(document.FORMNAME.group1[i].value=="correct"){alert("you guessed"); break /*arguably here you want to break this loop*/}


I can't understand what type of problem you're encountering with checkboxes (the main type of form element in case you allow multiple answers)

07-05-2002, 09:07 AM
Hi everyone,

I will try to spell it out as much as I can ::D

I have a number of checkboxes in a quiz - Each checkbox has a value of "1" if it is checked EXCEPT some of the questions where the correct answer is TO LEAVE THE CHECKBOX UNCHECKED ! Each checkbox is a group of its own based on its name (0,1,2 ..)

The quiz is validated using an array on a result-page :

correctAnswers = new Array('1','1','1','null ','null ','1','1','1','1','1','1',

NOTICE question 4 and 5 - they are correct if unchecked !

I was hoping to be able to validate the null value somehow - Only the above method doesnt cut it ???

Hope this clarified things a bit - The code is still available at :


Best Theo

07-05-2002, 10:26 PM
I noticed that, at least apparently, he might have trailing white spaces around:
correctAnswers = new Array('1','1','1','null ','null ', ...

null or
'null' or
'null ' ?

So maybe if that wasn't intentional, might be safer:

if (VariableThatSaysNoBoxChecked
&& (correctAnswers[i] != null || (typeof(correctAnswers[i])=="string" && correctAnswers[i].indexOf("null")!=-1))) {

maybe, probably messing up with brackets, anyway the idea is:
if !=null or
if typeof string
and if also indexOf("null")!=-1
bla bla

I hope this helps