Sometimes you need to change the order of your questions depending on Respondent’s answer. In the example below we will ask the gender of Respondent. The order of the next two questions will be changed depending on Respondent’s answer.
Try how it works below
In order to reproduce this example, please undertake the following steps:
Create a “Single answer” question where the Respondent is asked about his/her gender. Thereafter create two “Media slide” questions. Come back to the first question”, run the ScriptEditor and insert the following script into “OnNextPage script” tab (see image below).
|
![]() |
After that modify the tokens highlighted in red as described in the table below:
QUEST1_CODE | Code of the first media question |
QUEST2_CODE | Code of the second media question |
ALTER2_CODE | Code of the second alternative in the “Single answer” question |
Advanced explanations
- // in the beginning of our script we will write a small function that finds the index of question in the survey’s question list. This function accepts two parameters: quest - the position of question we want to find; size - the size of the list that contains all questions of the survey
- function getQuestionPosition (quest, size) {
- for (i = 0; i < size; i++) {
- if (q.items[i]==quest) {
- return i;
- }
- }
- return -1;
- }
- //get the code of the given answer. Note that “this” refers only to the current question
- var answer_code = this.get()[0].code;
- //here we compare the answer code with the code of second alternative. If they are equal, we change the order of questions
- if (answer_code=='ALTER2_CODE') {
- //here we get the size of the array that contains all survey questions
- var size = q.items.length;
- //get the reference to the first media question
- var q1 = q["QUEST1_CODE"];
- //get the reference to the second media question
- var q2 = q["QUEST2_CODE"];
- //find the index of the first media question
- var q1_index = getQuestionPosition(q1, size);
- //find the index of the second media question
- var q2_index = getQuestionPosition(q2, size);
- //change the order of questions
- q.items[q1_index] = q2;
- q.items[q2_index] = q1;
- return;
- }