Script API Tutorial

Filtering based on answers in «Matrix» question

Suppose that we’re asking Respondent whether he/she likes the products of some brands, and then (in the next question) we inquire about the reason.

In the example below we'll be interested in dislikes only (brands which the Respondent liked will not be displayed in the second question).

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

Go to Questionnaire editor and create a "Single answer matrix" question. Add some brands as alternatives into row options and some answers (for our example make them "Like" and "Don't like") into column options.

Create an "Open Ended" question and add the brands from the previous question as the alternatives for it. Come back to your "Single answer matrix" question, launch the Script Editor and insert the following script into "OnNextPage script" tab (see the image below):

  • var alternatives = q['SecondCode'].getAlternatives();
  • for (var i=0; i<alternatives.length; i++) {
  • var alternative = alternatives[i];
  • if (ifLike(alternative)){
  • alternative.hide();
  • }
  • }
  • function ifLike(alternative){
  • var answers = q['FirstCode'].get();
  • for (var i=0;i<answers.length;i++) {
  • var answer = answers[i];
  • var codes = answer.code.split(",");
  • var rowCode = codes[1];
  • var columnCode = codes[0];
  • if ((rowCode==alternative.code)&&(columnCode=='1')) {
  • return true;
  • }
  • }
  • return false;
  • }
  • var allHidden = true;
  • for (var i=0; i<alternatives.length; i++) {
  • var alternative = alternatives[i];
  • if (alternative.isVisible()) {
  • allHidden = false;
  • break;
  • }
  • }
  • if (allHidden) {
  • q['SecondCode'].hide();
  • }

Thereafter, replace SecondCode by the code of your "OpenEnded" question and FirstCode by the code of your "Single Matrix" question.

Advanced Explanations

The implementation algorithm is very simple. We take alternatives of the "OpenEnded" question and check each brand with respect to whether Respondent likes it or not.

In our example we will hide the alternatives which contain the brands liked by Respondent. It the end we check whether all alternatives are hidden. If yes, we hide the "OpenEnded" question.

  • var alternatives = q['SecondCode'].getAlternatives();
  • // get the alternatives of the "OpenEnded" question
  • for (var i=0; i<alternatives.length; i++) {
  • var alternative = alternatives[i];
  • // for each alternative check if Respondent likes the brand inside the "ifLike" function
  • if (ifLike(alternative)) {
  • alternative.hide();
  • // hide alternative if Respondent likes it;
  • }
  • }
  • function ifLike(alternative)
  • {
  • var answers = q['FirstCode'].get();
  • // get Respondent’s answers to the "Single Matrix" question
  • for (var i=0;i<answers.length;i++) {
  • var answer = answers[i];
  • // for the each answer
  • var codes = answer.code.split(",");
  • var rowCode = codes[1];
  • var columnCode = codes[0];
  • // Answer object has a property 'code'. For matrix question the code is represented by string with two tokens separated by comma. The first token is the code of chosen column and the second token is the code of chosen row. In our example "Don't like" is represented by the column with code '1'
  • if ((rowCode==alternative.code)&&(columnCode=='1')) {
  • //check whether the code of row equals to the code of alternative and the code of column is equal to '1'
  • return true;
  • //if yes, then return true (means that Respondent likes the brand)
  • }
  • }
  • return false;
  • //if not, return false (means that Respondent does not like the brand)
  • }
  • // check whether or not all alternatives are hidden and if it's true, hide the question
  • var allHidden = true;
  • // initially we assume that all alternatives are hidden.
  • for (var i=0; i<alternatives.length; i++) {
  • var alternative = alternatives[i];
  • if (alternative.isVisible()) {
  • allHiden = false;
  • break;
  • // if any alternative is not hidden then assign “false” value to the "allHidden" variable
  • }
  • }
  • if (allHidden) {
  • //if 'allHidden' indicator is false, then hide "Open Ended" question
  • q['SecondCode'].hide();
  • }

Learn more at CoolTool knowledge base