Welcome, Guest
Username: Password: Remember me

TOPIC: Change question text based on a previous answer

Change question text based on a previous answer 2 years 11 months ago #60417

  • novascape
  • novascape's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 4
  • Thank you received: 1
  • Karma: 0
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: 2 years 11 months ago by novascape.
The administrator has disabled public write access.

Re: Change question text based on a previous answer 2 years 11 months ago #60424

  • novascape
  • novascape's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 4
  • Thank you received: 1
  • Karma: 0
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: www.limesurvey.org/en/forum/can-i-do-thi...it=10&start=10#57392

Steps:
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");
objElement.style.visibility = "visible";
objElement.innerHTML = "XXX-Dynamic Text-XXX";
});
</script>

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";
}
});
</script>

Hope this helps someone else
Regards
Last Edit: 2 years 11 months ago by novascape.
The administrator has disabled public write access.
The following user(s) said Thank You: Calidonia
Moderators: ITEd
Time to create page: 0.096 seconds
Donation Image