Check out the LimeSurvey source code on GitHub!

Change question text based on a previous answer

5 years 9 months ago - 5 years 9 months ago #60417 by novascape
Hi all,

How can I change the text of a question dynamically based on the answer of a previous question? I have a question that should be phrased in one of two possible ways, depending on how the user answered a previous question.

Can I maybe use a replacement field and somehow set up the value for that replacement field depending on a previous answer?

Using 1.91RC6

Thanks in advance
Last Edit: 5 years 9 months ago by novascape.

Please Log in to join the conversation.

5 years 9 months ago - 5 years 9 months ago #60424 by novascape
I believe I have found the answer to my own question: You have to use javascript inside the question text to dynamically set the question text where the javascript will use a previous question answer token embedded inside the actual javascript.

This post gave me the hint I was looking for:

1. Go to global settings and set the setting 'XSS-Filter' to 'off' to allow javascript

2. Edit the question text, position the cursor at say the end of your question text and click on the last [Insert/Edit Limesurvey replacement field] button to select the answer of a previous question that you wish to use in the conditional logic. It will insert something like {INSERTANS:87647X2X17} which will be replaced at runtime with the actual text from that answer

3. Now click on the first [Toggle fullscreen...] toolbar button

4. When the big editor comes up, click on the [Source] button to show the HTML source

5. Add a DIV tag somewhere within your text that you want to have replaced dynamically at rune time, e.g. <DIV id="dynamicText"></DIV>

6. Add the following javascript at the end of your question text:

<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
var objElement = document.getElementById("dynamicText"); = "visible";
objElement.innerHTML = "XXX-Dynamic Text-XXX";

7. If you now close the source editor and save everything and preview this question you will see the DIV tag being replace by the "XXX-Dynamic Text-XXX" above

8. You can now move the {INSERTANS:87647X2X17} token into the javascript to use in conditional logic. Here is the final sample version of the source for my question (puts the two possible versions of text in 2 div tags and then toggles the visibility of those tags based on the previous question answer):

<div id="q4a">Question Text 1</div>
<div id="q4b">Question Text 2</div>
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
var q2Answer = "{INSERTANS:87647X2X17}";
if (q2Answer == "No") {
document.getElementById("q4a").style.visibility = "visible";
document.getElementById("q4b").style.visibility = "hidden";
} else {
document.getElementById("q4a").style.visibility = "hidden";
document.getElementById("q4b").style.visibility = "visible";

Hope this helps someone else
Last Edit: 5 years 9 months ago by novascape.
The following user(s) said Thank You: Calidonia

Please Log in to join the conversation.

Imprint                   Privacy policy         General Terms & Conditions         Revocation information and revocation form