Javascript Conditions

👨‍💻️ What are Javascript conditions ?

Using javascript conditions you can define your conditions rules using the Javascript language. The javascript conditions free you up from the predefined conditions offered by the condition rules user interface and gives you a complete freedom to write your own conditions using native Javascript statements and operators.

The code of your javascript condition must return either true if the condition rule should be successfully evaluated or false it it failed.

You may use following presets objects in your code

  • respondentAnswers are the current respondent answers array.

  • languageCode is the current selected language string of the respondent.

  • answers are the current answers array of the survey.,

RespondentAnswer {
  answerId: string;
  respondentId: string;
  sectionNumber: number;
  value: string;
  disabled: boolean;
  authorizationToken: string;
}
 Answer {
  id: string;
  questionId: string;
  answerTypeId: string;
  text: string;
  pipeAlias:string;
  }

Following code will loop through the current respondent answers and check that the answer with id "answerid1234" has been selected by the respondent.

for (var i=0;i<respondentAnswers.length;i++){
  if (respondentAnswers[i].answerId == 'answerid1234') {
     return true;
  }
}
return false;

You may use the question / answer helper function selector to get the questions or answers id

You may use the Test code button to test your code with an empty set of respondent answers.

📚 Helper functions

To avoid lengthy repetitive code you may also use following helper functions.

  • isAnswerAnswered(answerId, textValue) returns true if the answer has been selected or entered by the respondent. You may also supply the option textValue to compare the respondent entered text to a given text.

  • isQuestionAnswered(questionId) will return true if any answers has been selected or entered for the question.

Following sample checks that the answer with id "MCf35sd_UXTarDERxRHa" has its value set to ngSurvey.

return isAnswerAnswered('MCf35sd_UXTarDERxRHa', 'ngSurvey');

⚠️ Known limitations

Please note that this feature has currently following limitations that should be lifted in a coming release.

  • The engine currently supports only ECMAScript 5. As such do not use any language statements that are not based on ECMAScript 5 standards.

  • As the javascript conditions are running as well on the server side do not use any browser specific objects like window, console etc.. as these can't be evaluated on the server side as there is no browser.

  • If you make a copy of your survey using the clone features you will need to set back the question and answers ids of your javascript condition manually to the newly created one in your cloned survey.

If you really need to use browser objects and the latest Javascript language statements / operators you may do so by turning off the server side validation in your survey properties. Note that in such a case no more server side validation will be done to enforce the question / answers validity of your respondent answers.

Last updated

(c) 2024 Data Illusion Zumbrunn. All rights reserved. Reproduction strictly forbidden.