How to use JavaScript in display logic for questions?


In some instances question display logic may be more complex than the basic logic builder can handle. Some examples may include computation on previous answers (such as computing BMI from height and weight), precise display probabilities (e.g. 30% of respondents should see the question), etc.

Display logic condition menu

The basic format of this function is:

function test() {
    return [CONDITION TO BE TESTED];
}

The test() function should contain all the calculations and return a boolean value, which will determine whether the question is shown (if true is returned the question is displayed).

Display logic JavaScript example

Example 1: Show the question to approximately half of respondents

function test() {
   return Math.random() < 0.5;
}

Example 2: Show on mobile only

function test() {
   return ($(window).width() <= 480);
}

Example 3: Show on desktop only

function test() {
   return ($(window).width() > 480);
}
how to use javascript

Using answers to other questions

To refer to previous answers in the survey, you must first identify the question field id via your browser’s developer tools, these might be something like id='additionalQuestions1-short-answer', in which case you could access the value with: $("#additionalQuestions1-short-answer").val(). This value could then be processed with JavaScript and JQuery functions.