I want to build a survey which has about 20 questions. All questions are in the same page.
The structure of whole survey is like the tree structure.
Initially, only one question is shown in the page.
if user chooses option 1,2 of question 1, the survey ends. if user chooses option 3,4,5 of question 1, then question 2 appears.
if user chooses option 1,2,6 of question 2, then question 3 appears, otherwise go to question 12.
if user chooses 1,2 of question 3, go to question 5, otherwise go to question 4.
....
It's quite troublesome to switch questions (using display:none and display:block) by javascript when there are lots of questions.
What is the simplest way in this situation?
Following code is written according to JacobM's anwer.
Another problem appears.
I need to hide and show another question if user chooses another options.
option 1 of 2a -> show q12
option 4 of 2a -> show q3 and hide q12
option 1 of q3 -> show q4
option 1 of 2a -> show q12, hide q3 and q4 <--- this situation is quite complex
What should I do?
var responses = {
1 : {1:'end', 2:'end', 3:2, 4:2, 5:2, 6:2},
'2a': {1:12,2:12,3:3,4:3,5:3,6:12}
};
function handleResponse(question, key) {
option = question.selectedIndex+1;
var next = responses[key][option];
if (next=='end') return;
showQuestion(next);
}
function showQuestion(question){
$('tr[id^=trQ'+question+']').show();
}