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 Coder
    Join Date
    Apr 2005
    Posts
    97
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Dynamic variables, sort of

    Hey,
    I have a dynamically generated quiz with a variable number of questions. I need to do validation on these questions.. They are named like
    question1, question2,question3,...etc.

    Code:
    function grade(b)
        {
          var numRight = 0;
          for(var i=0; i< questions[b].length; i++)
          {
            var answer = getChecked(document.quizform.question....?);
            alert(answer);
            if(answer == cor_ans[b][i])
            {
              numRight = numRight + 1;
            }
    
          }
          alert(numRight);
    
        }
    My question is how do I check each question using the document.quizform.questionX?

    Thanks!

  • #2
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Use the form's elements object and array notation.
    Code:
    var answer = getChecked(document.quizform.elements['question'+b]);
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    JSON might help you, i presume
    http://www.json.org/
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #4
    New Coder
    Join Date
    Apr 2005
    Posts
    97
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thank you both.

    That worked perfectly.


  •  

    Posting Permissions

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