Check out the LimeSurvey source code on GitHub!

Information from previous answers

More
5 years 10 months ago #59196 by COMPGURL
Why can't LimeSurvey insert the answer of a previous question into the text of another question on the same page?
docs.limesurvey.org/Adding+a+question&st...rom_previous_answers

Is there a possible workaround for this?

Please Log in to join the conversation.

More
5 years 10 months ago #59213 by tpartner
You would need to use JavaScript to do that. Details of the workaround would depend on the question types and what you would want as default text (inserted if there is no answer to the previous question).

Can you give more details or point us to a sample survey?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

More
5 years 10 months ago - 5 years 10 months ago #59227 by COMPGURL
Here's an example:

One question requires the user to input a numerical value.
ie: How many apples to do you have? (answer1)

The next question is a list (radio) question that displays the previous value.
ie: Do you have (answer1) apples?
Last Edit: 5 years 10 months ago by COMPGURL. Reason: corrected grammar

Please Log in to join the conversation.

More
5 years 10 months ago #59235 by tpartner
Okay.

1) Set up your survey to use JavaScript .

2) Add a span to the text of Q1 that contains the text to be modified. so the source of Q1 may look like:
Do you have <span class="insertedText">any</span> apples?

3) Add the following script to the source of one of the question. Replace the following variables in lines 6-8 as necessary:
- 11 = the Q1 ID
- 22 = the Q2 ID
- "any" = the text to be inserted if Q1 is left blank

The script initially looks for a value for Q1 and, if one is found it is inserted into the span element of Q2. There is a listener on the Q1 input to do the same thing when it is changed. If the Q1 input is emptied, a default text is inserted into the span element.
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function(){
 
		// The question IDs
		var q1ID = 11;
		var q2ID = 22;
		var defaultText = 'any';
 
		// Initially modify the insertedText span if there is a value for Q1
		if($('#question'+q1ID+' input.text').val()) {
			$('#question'+q2ID+' span.insertedText').text($('#question'+q1ID+' input.text').val());
		}
 
		// A Listener on the Q1 input to modify the insertedText span
		$('#question'+q1ID+' input.text').change(function(){ 
			if($('#question'+q1ID+' input.text').val()) {
				$('#question'+q2ID+' span.insertedText').text($('#question'+q1ID+' input.text').val());
			}
			else {
				$('#question'+q2ID+' span.insertedText').text(defaultText);
			}
		});
 
	});
 
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

More
5 years 10 months ago #59276 by COMPGURL
I'm not sure where use the q1 and q2 values in this JavaScript.

$(document).ready(function(){
var q1ID = 12345X12X1234;
var q2ID = 12345X12X1235;
var defaultText = 'apples';


if($('#question1234'+q1ID+'input.text').val())
{
$('#question1235'+q2ID+'span.insertedText').text$('#question1235'+q1ID+'input.text').val());
}

$('#question1234'+q1ID+'input.text').change(function()
{
if($('#question1234'+q1ID+'input.text').val())
{ $('#question1235'+q2ID+'span.insertedText').text($('#question1235'+q1ID+'input.text').val()); }

else
{
$('#question1235'+q2ID+'span.insertedText').text(defaultText); } });

Please Log in to join the conversation.

More
5 years 10 months ago - 5 years 10 months ago #59277 by tpartner
You only need to modify the IDs in lines 6 and 7, do not modify anything below "var defaultText = 'any';". Use the question IDs only, not the full SGQA.


So, in the case of the attached image, your code may look like:
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function(){
 
		// The question IDs
		var q1ID = 13;
		var q2ID = 14;
		var defaultText = 'any';
 
		// Initially modify the insertedText span if there is a value for Q1
		if($('#question'+q1ID+' input.text').val()) {
			$('#question'+q2ID+' span.insertedText').text($('#question'+q1ID+' input.text').val());
		}
 
		// A Listener on the Q1 input to modify the insertedText span
		$('#question'+q1ID+' input.text').change(function(){ 
			if($('#question'+q1ID+' input.text').val()) {
				$('#question'+q2ID+' span.insertedText').text($('#question'+q1ID+' input.text').val());
			}
			else {
				$('#question'+q2ID+' span.insertedText').text(defaultText);
			}
		});
 
	});
 
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Attachments:
Last Edit: 5 years 10 months ago by tpartner.

Please Log in to join the conversation.

More
5 years 10 months ago #59278 by COMPGURL
It worked!! Thanks for your help...
:)

Please Log in to join the conversation.

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