Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Populate a question based on previous one

  • victorgil
  • victorgil's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
10 years 5 months ago #100075 by victorgil
Hi

First of all, I have to say that I've been looking for an answer (forum, google...) before asking you :( I'll explain my problem briefly

Page 1. Question 1. Age (Number)
Page 2. Question 2. Age Group (Hidden question that I need to set quotas and screen outs)
- Subquestion 1. <20
- Subquestion 2. >20

Have you an idea how to do this? I have to say that I'm a newbie

Many thanks in advance
The topic has been locked.
More
10 years 5 months ago #100076 by w0928
Replied by w0928 on topic Populate a question based on previous one
When you say 'populate a question'... do you mean show a question or pre-populate the answer to the question?

If it is the former, have you tried having 2 "question 2s" and showing one when question 1 has an answer of <20 and showing the other when question 1 has an answer > 20?

If it is the latter, I'm pretty sure there is a WorkAround in the manual that describes how to do that.
The topic has been locked.
  • victorgil
  • victorgil's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
10 years 5 months ago #100078 by victorgil
Replied by victorgil on topic Populate a question based on previous one
Many thanks whowe1 I mean pre-populate the answer of question 2. For example, If a respondent write 24 in question 1, then question 2 should be auto-checked as >20 (years old). I've spent all this evening a WorkAround in the manual and forum post, but I didn't find an example to use. Maybe I am not looking for correctly, but....I have nothing :( Thanks a lot for your support.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
10 years 5 months ago - 10 years 5 months ago #100090 by tpartner
Replied by tpartner on topic Populate a question based on previous one
Assuming:
1) the question code for the first question is "age"
2) the second question is a select-radio type
3) the second question is NOT hidden with the advanced setting

Try adding this to the source of the second question (not tested)
Code:
<script type="text/javascript" charset="utf-8">  
  $(document).ready(function() {  
 
    $('#question{QID}').hide();
 
    var age = '{age.shown}';
 
    if(age < 20) {
      $('#question{QID} input.radio:eq(0)').trigger('click');
    }
    else {
      $('#question{QID} input.radio:eq(1)').trigger('click');
    }
  });
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 10 years 5 months ago by tpartner.
The topic has been locked.
More
10 years 5 months ago - 10 years 5 months ago #100092 by Ben_V
Replied by Ben_V on topic Populate a question based on previous one
Hello Tony,

a small typo

var age = '{age.shown}' :
var age = '{age.shown}' ;

Benoît

EM Variables => bit.ly/1TKQyNu | EM Roadmap => bit.ly/1UTrOB4
Last Releases => 2.6x.x goo.gl/ztWfIV | 2.06/2.6.x => bit.ly/1Qv44A1
Demo Surveys => goo.gl/HuR6Xe (already included in /docs/demosurveys)
Last edit: 10 years 5 months ago by Ben_V.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
10 years 5 months ago #100093 by tpartner
Replied by tpartner on topic Populate a question based on previous one
Thanks Ben :)

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
More
10 years 5 months ago #100107 by nfuria
Replied by nfuria on topic Populate a question based on previous one
I tested but it doesn't works.
First the console report a "Unexpected identifier" at the line of the var age, as it becomes:

var age = '<span id='LEMtailor_Q_8707_49'></span>';

I change the script to include the var in double quotes:

var age = "{age.shown}";

With this change there are no error but the script doesn't works.

Attached the survey structure.

File Attachment:

File Name: hbc70170.lss
File Size:13 KB
Attachments:
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
10 years 5 months ago #100108 by tpartner
Replied by tpartner on topic Populate a question based on previous one
What is the question code for your age question?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
More
10 years 5 months ago - 10 years 5 months ago #100110 by nfuria
Replied by nfuria on topic Populate a question based on previous one
The code is "age".
The second question:
Selection:
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
$('#question{QID}').hide();
var age = "{age.shown}";
console.log(age);
if(age < 20) {
$('#question{QID} input.radio:eq(0)').trigger('click');
}
else {
$('#question{QID} input.radio:eq(1)').trigger('click');
}
});
</script>
Last edit: 10 years 5 months ago by nfuria. Reason: pasted the wrong code
The topic has been locked.
More
10 years 5 months ago #100112 by nfuria
Replied by nfuria on topic Populate a question based on previous one
The attachment I upload before has the single quotes in the "var age" and thus cause the "unexpected identifier" error.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
10 years 5 months ago #100122 by tpartner
Replied by tpartner on topic Populate a question based on previous one
You initially said these questions are on different pages but the sample survey has them on the same page. Which do you need?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
More
10 years 5 months ago #100127 by nfuria
Replied by nfuria on topic Populate a question based on previous one
The original question is posted by another user.
I tested the solution posted presuming that the hidden question can stay in the same page.
Now I change the format of the survey to "Question by question" and it works.
Also, it works with single or double quotes in var age.

Actually I tested because I had a similar situation but I need that the question stay in the same page.
Are there some solution?
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose