Welcome, Guest
Username: Password: Remember me

TOPIC: Information from previous answers

Information from previous answers 3 years 2 weeks ago #59196

  • COMPGURL
  • COMPGURL's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 14
  • Karma: 0
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?
The administrator has disabled public write access.

Re: Information from previous answers 3 years 2 weeks ago #59213

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 3813
  • Thank you received: 683
  • Karma: 328
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.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.

Re: Information from previous answers 3 years 2 weeks ago #59227

  • COMPGURL
  • COMPGURL's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 14
  • Karma: 0
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: 3 years 2 weeks ago by COMPGURL. Reason: corrected grammar
The administrator has disabled public write access.

Re: Information from previous answers 3 years 2 weeks ago #59235

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 3813
  • Thank you received: 683
  • Karma: 328
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.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.

Re: Information from previous answers 3 years 1 week ago #59276

  • COMPGURL
  • COMPGURL's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 14
  • Karma: 0
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); } });
The administrator has disabled public write access.

Re: Information from previous answers 3 years 1 week ago #59277

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 3813
  • Thank you received: 683
  • Karma: 328
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.
Capture_2011-04-06.PNG


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.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
Last Edit: 3 years 1 week ago by tpartner.
The administrator has disabled public write access.

Re: Information from previous answers 3 years 1 week ago #59278

  • COMPGURL
  • COMPGURL's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 14
  • Karma: 0
It worked!! Thanks for your help...
:)
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.162 seconds
Donation Image