Script API Tutorial

Saving the order of Respondent's answers

Sometimes it makes sense to save the order of Respondent’s answers for further analysis. In the example below we will ask Respondent about chocolate brands that he/she ever tasted and save the order of his answers.

In addition, we will pre populate the answer options in the next question, observing the order of saved answers.

Give it a try

In order to reproduce this example, please undertake the following steps:

Go to Questionnaire editor and add "Multiple answers" question. Add "Brand1", "Brand2", "Brand3" and "Brand4" as alternatives.

Then create "Ranking" question with the same alternatives.

After that open Script Editor in the "Ranking" question and insert the following script into "OnRender script" tab (see the image below)

  • var insertAfterCode = null;
  • var q1answers = q['FQuestCode'].get();
  • var alters = q['SQuestCode'].alternatives;
  • var length = q1answers.length;
  • for (var i=0; i<length; i++) {
  • var answer = q1answers[i];
  • if (insertAfterCode!=null){
  • setPosition(answer.code, insertAfterCode)
  • }
  • insertAfterCode = answer.code;
  • }
  • length = alters.length;
  • for (var i=0;i<length;i++) {
  • this.set(alters[i].code, true);
  • }
  • function setPosition(code, insertAfterCode) {
  • var length = alters.length;
  • for (var i=0;i<length;i++) {
  • if (alters[i].code==code){
  • alters[i].insertAfter(insertAfterCode);
  • }
  • }
  • }

Substitute FQuestCode by the code of "Multiple Answers" question and SQuestCode by the code of "Ranking" question.

Then switch to "Item setup script" tab (see the image below) and add the following script:

this.filter('FQuestCode');

Replace FQuestCode by the code of your "Multiple Answers" question.

Advanced Explanations

The idea of saving order of answers is very simple. We create a hidden question (in our example it was not hidden, as we had to demonstrate its work) with the same alternatives as in previous question. Those alternatives will be sorted based on the order of Respondent's answers.

To sort alternatives of the hidden question we use "insertAfter('insertAfterCode')" function that inserts one alternative after another When alternatives of the hidden question are sorted, we emulate Respondent's answers and choose them all.

  • var insertAfterCode = null;
  • // each alternative, except the first one, has a predecessor in the list. 'insertAfterCode' variable contains the code of the predecessor. As the first alternative has no predecessor, we initialize this variable with null value
  • var q1answers = q['FQuestCode'].get();
  • // get answers that we intend to save
  • var alters = q['SQuestCode'].alternatives;
  • // get alternatives of the current (hidden) question
  • var length = q1answers.length;
  • // get number (quantity) of the answers
  • for (var i=0; <length; i++) {
  • // for each answer
  • var answer = q1answers[i];
  • // get specific answer
  • if (insertAfterCode!=null){
  • // there's no need in moving alternative that coincides with the first answer
  • // so we check whether alternative has a predecessor... and if it has
  • setPosition(answer.code, insertAfterCode)
  • // we set new position for the alternative in the ‘setPosition’ function (see below )
  • }
  • insertAfterCode = answer.code;
  • // set current answer code as the code of the predecessor
  • }
  • // after alternatives are sorted, we emulate Respondent’s answer. To do it we use 'set()' function of the 'Questionnaire' object
  • length = alters.length;
  • for (var i=0;i<length;i++) {
  • // for each alternative
  • this.set(alters[i].code, true);
  • // set current alternative as the answer
  • }
  • // "setPosition" function will place specific alternative in line with Respondent’s answer. It accepts two variables as the parameter. 'Code' - code of the answer. 'insertAfterCode' - code of the predecessor.
  • // We can search the alternative by 'Code' and place it after alternative specified by 'insertAfterCode'
  • function setPosition(code, insertAfterCode) {
  • var length = alters.length;
  • for (var i=0;i<length;i++) {
  • // for each alternative
  • if (alters[i].code==code){
  • // if alternative code coincides with the answer code
  • alters[i].insertAfter(insertAfterCode);
  • // move it
  • }
  • }
  • }

Learn more at CoolTool knowledge base