Script API Tutorial

Answer piping

One of the most important and popular tasks faced by researchers is Question or Alternative piping. Please, have a look at the example of Alternative Piping below.

Here we ask a Respondent about his knowledge of some chocolate products in the first question, then display his answers in the next one.

Type something in 'Other' field and you will be able to see how piping works.

Give it a try

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

Go to Questionnaire editor and create 'Multiple answer' question. Add four alternatives.

Name the first three alternatives: "Brand1", "Brand2", "Brand3". For the fourth alternative set the name "Other" and enable "Other option" checkbox (see the image below)

Then insert the following script into "Global setup script" tab (see the image below)

  • window.getOther = function(){
  • var results = q['QuestCode'].get();
  • var length = results.length;
  • for (i = 0; i <length; i++){
  • var answer = results[i];
  • if (answer.code=='OtherAlternativeCode') {
  • return answer.value;
  • }
  • }
  • }

After that modify the tokens highlighted in red as described in the table below:

QuestCode Code of the Question
OtherAlternativeCode Code of the "Other" Alternative

Come back to Questionnaire editor and add another Question (select "Rating" type). Add four alternatives.

Name the first three similarly: Brand1, Brand2, Brand3, while the name of your fourth alternative will be {{getOther()}}

Insert the following script into "Item setup script" tab (see the image below):

this.filter('QuestCode');

and replace 'QuestCode' token (highlighted in red) by the code of your previously created question.

Run your Questionnaire and see the results!

Advanced explanations

Let’s go through our scripts step by step:

Script One

  • window.getOther = function(){
  • // we define 'getOther' variable and store it in the browser. This variable is a function which actually performs the piping
  • var results = q['QuestCode'].get();
  • // we get Respondent’s answers and assign them to the result variable
  • var length = results.length;
  • // get the number of answered alternatives
  • for (i = 0; i <length; i++){
  • // for all answers
  • var answer = results[i];
  • // get specific answer
  • if (answer.code=='OtherAlternativeCode') {
  • // if the code of answer is equal to the code of "Other" alternative
  • return answer.value;
  • // return the text entered by Respondent as the result
  • }
  • }
  • }

Script two

{{getOther()}}

// here we invoke the script created before. The returned data is displayed as the name of Alternative

Script three

this.filter('QuestCode');

//There are four alternatives in the first question . However, the Respondent may not always select all of them, thus the number of answers can vary from 1 to 4.

//In the second question we want to show only those alternatives which were selected in the first one (simply put, we want to filter the alternatives in the second question). For that purpose we use "filter" method.

Learn more at CoolTool knowledge base