Script API Tutorial

Filtering answers in «Matrix» question

In the article above we demonstrated how the alternatives are filtered depending on previous answers. Now let’s study a more complicated example where we need to filter alternatives in a Matrix Question.

Pass the survey below and see how it works:

Give it a try

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

Go to Questionnaire Editor and create a couple of "Multiple answer" questions. Create three alternatives - "Card1", "Card2" and "Card3" in the first question and three alternatives - "Bank1", "Bank2" and "Bank3" - in the second one.

Create “Multiple answer matrix” question. Enter "Card1", "Card2" and "Card3" as your row options and "Bank1", "Bank2" and "Bank3" as your column options. Launch Script Editor and insert the following script into "OnRender script" tab (see the image below)

  • var firstAnswers = q['CodeOfTheFirstQuestion'].get();
  • var secondAnswers = q['CodeOfTheSecondQuestion'].get();
  • var alts = this.getMatrixAlternativesY();
  • for (var i=0; i<alts.length; i++) {
  • this.hideRow(alts[i].code);
  • }
  • for (var i=0; i<alts.length; i++) {
  • if (firstAnswers.contains(alts[i].code)) {
  • this.showRow(alts[i].code);
  • }
  • }
  • alts = this.getMatrixAlternativesX();
  • for (var i=0; i<alts.length; i++) {
  • this.hideColumn(alts[i].code);
  • }
  • for (var i=0; i<alts.length; i++) {
  • if (secondAnswers.contains(alts[i].code)) {
  • this.showColumn(alts[i].code);
  • }
  • }

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

CodeOfTheFirstQuestion Code of your first "Multi answer" question
CodeOfTheSecondQuestion Code of your second "Multi answer" question
firstAnswers Variable that contains Respondent's answers to the first question. In this example we named it "firstAnswers", although you are free to name it differently.
secondAnswers Variable that contains Respondent's answers to the second question. In this example we named it "secondAnswers", although you are free to name it differently.

Advanced explanations

Let us go through abovementioned script line by line:

  • var firstAnswers = q['CodeOfTheFirstQuestion'].get();
  • // we get the answers to the first question by using "get()" function and assign them to "firstAnswers" variable
  • var secondAnswers = q['CodeOfTheSecondQuestion'].get();
  • // we get the answers to the second question by using 'get()' function and assign them to "secondAnswers" variable
  • var alts = this.getMatrixAlternativesY();
  • // we get the row options and assign them to "alts" (alternatives) variable
  • for (var i=0; i<alts.length; i++) {
  • // we iterate over row options
  • this.hideRow(alts[i].code);
  • // and hide them
  • }
  • for (var i=0; i<alts.length; i++) {
  • // we iterate over row options again
  • if (firstAnswers.contains(alts[i].code)) {
  • // if answer to the first question matches the row option
  • this.showRow(alts[i].code);
  • // we make this row visible
  • }
  • }
  • alts = this.getMatrixAlternativesX();
  • // we get the column options and assign them to "alts" variable (since variable "alts" already exists, we don't have to use token "var" to define it)
  • for (var i=0; i<alts.length; i++) {
  • // we iterate over column options
  • this.hideColumn(alts[i].code);
  • // and hide them
  • }
  • for (var i=0; i<alts.length; i++) {
  • // we iterate over column options again
  • if (secondAnswers.contains(alts[i].code)) {
  • // if answer to the second question matches the column option
  • this.showColumn(alts[i].code);
  • // we make this column visible
  • }
  • }

Learn more at CoolTool knowledge base