Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jul 2002
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    null value = correct answer in checkbox javascript quiz ?

    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 :

    http://www.jensens-bureau.dk/webpub4/grundquiz/

    Yup, - thats danish !!!



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

    Best Theo Denmark

  • #2
    New Coder
    Join Date
    Jun 2002
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ciao

    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:

    document.FORMNAME.group1[0]
    document.FORMNAME.group1[1]
    document.FORMNAME.group1[2]

    document.FORMNAME.group2[0]
    document.FORMNAME.group2[1]
    document.FORMNAME.group2[2]

    To see if one is checked:

    document.FORMNAME.group1[0].checked
    returns true if checked, false if not.

    You can use loops:

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

    etc...

    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)
    Alberto http://www.unitedscripters.com/

  • #3
    New to the CF scene
    Join Date
    Jul 2002
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool

    Hi everyone,

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

    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',
    '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 :

    http://www.jensens-bureau.dk/webpub4/grundquiz/

    Best Theo

  • #4
    New Coder
    Join Date
    Jun 2002
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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))) {
    //stuff
    }

    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
    ciao
    Alberto http://www.unitedscripters.com/


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •