4
function HideSectionTwo() {

    // If correct answers are checked 
    if(document.getElementById("S1Q1A").checked == true)  {
        document.getElementById("sectiontwo").style.display = "";

    }
    //else if nothing is checked
    else {
        document.getElementById("sectiontwo").style.display = "none";
    } 
}

I'm new to JavaScript, but i'm trying to create a multiple choice quiz in with 4 questions in each section. Once all the answers have been correctly answered in that section then it will display the next section. For my HTML I'm using tables and have the entire section stored in these individual tables which have id's labeled "sectionone", "sectiontwo" and so on. The code above is what I have been working from. I tried getElementByClass and assigned my elements names by class, but it didn't work. I'm just kind of stuck and need some insight on which direction to go.

My script works for selecting single answers. If i select Section 1 Question 1 answer A the table that has an id of "sectiontwo" will display.

3
  • 2
    Hi! Can we see your HTML? Commented Jul 29, 2014 at 2:11
  • What exactly is the issue here? Commented Jul 29, 2014 at 2:22
  • Are you asking how to keep a section up until all four correct answers are checked? Commented Jul 29, 2014 at 2:38

1 Answer 1

1

You can try using a variable that keeps track of how many questions are filled in a section, and then checking that to decide whether to display the next section. Something like:

//count of questions answered
var s1count = 0;
//question 1
if (document.getElementById('S1Q1A').checked || document.getElementById('S1Q1B').checked || document.getElementById('S1Q1C').checked || document.getElementById('S1Q1D').checked) {
  s1count++;
}
//question 2
if (document.getElementById('S1Q2A').checked || document.getElementById('S1Q2B').checked || document.getElementById('S1Q2C').checked || document.getElementById('S1Q2D').checked) {
  s1count++;
}
//and so on...

//check the count, say if section 1 has 5 questions
if (s1count == 5) {
  document.getElementById("sectiontwo").style.display = "block"; //or inline
}
Sign up to request clarification or add additional context in comments.

1 Comment

After a little customizing this worked perfectly. Just needed a nudge in the right direction. Thanks so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.