- Posts: 13
- Thank you received: 0
Welcome to the LimeSurvey Community Forum
Ask the community, share ideas, and connect with other LimeSurvey users!
Answer options based on previous question
- AdinaNica1
- Offline
- New Member
Less
More
6 years 5 months ago - 6 years 5 months ago #160316
by AdinaNica1
Replied by AdinaNica1 on topic Answer options based on previous question
Hi!
I have a similar situation. I have tried to use the script that you shared and it didn't work (once you tick a certain answer in q1 it doesn't modify the options in Q2). I also tried to use your test survey, and it is the same result, no change. Is there anything else I should have done? Is this working only for certain versions?My version is 2.67.3+170728.
Many many thanks!
I have a similar situation. I have tried to use the script that you shared and it didn't work (once you tick a certain answer in q1 it doesn't modify the options in Q2). I also tried to use your test survey, and it is the same result, no change. Is there anything else I should have done? Is this working only for certain versions?My version is 2.67.3+170728.
Many many thanks!
Last edit: 6 years 5 months ago by AdinaNica1.
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10099
- Thank you received: 3589
6 years 5 months ago #160345
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Answer options based on previous question
This script will work in 2.7.x:
Sample survey attached:
Code:
<script type="text/javascript" charset="utf-8"> $(document).ready(function(){ // The Q2 options corresponding to the Q1 options teacherLists = { C1 : 'T1,T3,T5', C2 : 'T1,T2,T3,T4', C3 : 'T2,T3,T4,T5', C4 : 'T1,T4,T6' }; // Identify this question var thisQuestion = $('#question{QID}') // The checked option in Q1 var q1Ans = '{Q1}'; // Hide all of the teachers $('.answer-item', thisQuestion).hide(); // Now show the appropriate ones $(teacherLists[q1Ans].split(',')).each(function(i){ $('.answer-item[id$="'+{QID}+this+'"]').show(); }); // Uncheck all hidden radios (in case page has been seen before) $('.answer-item:hidden input.radio', thisQuestion).prop('checked', false); }); </script>
Sample survey attached:
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Attachments:
The following user(s) said Thank You: AdinaNica1
The topic has been locked.
- AdinaNica1
- Offline
- New Member
Less
More
- Posts: 13
- Thank you received: 0
6 years 5 months ago - 6 years 5 months ago #160348
by AdinaNica1
Replied by AdinaNica1 on topic Answer options based on previous question
Thank you so much! I uploaded the demo and, when I click C1 in the first question, here is what it shows in the second page. Please see the attachment with the screenshot! In the meantime I have upgraded to 2.72 version and there is the same result
Is there anything I should do?
Adina
Is there anything I should do?
Adina
Attachments:
Last edit: 6 years 5 months ago by AdinaNica1.
The topic has been locked.
- Joffm
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 12881
- Thank you received: 3965
6 years 5 months ago #160358
by Joffm
Volunteers are not paid.
Not because they are worthless, but because they are priceless
Replied by Joffm on topic Answer options based on previous question
Hello,
in this case - filter a single punch question on a single punch question - I have a different approach.
To be honest: I prefer out of the box solutions just because of my lack of JS-knowledge.
Okay, Tony's script here is short, straight forward and easy to understand.
Let me explain:
Between the two single questions (here: course Q1 and teacher Q2) I insert an equation and a multiple question M1 (both hidden).
The multiple question with exactly the same answer options and codes as the second single ("Teachers") is used to be the matrix filter.
By the equation I preset the multiple question: e.g.
{M1_T1=if((Q1=="C1") OR (Q1=="C3"),"Y","")}
{M1_T2=if((Q1=="C2") OR (Q1=="C4") OR (Q1=="C5"),"Y","")}
...
And the matrix filter of Q2 is M1.
Best reqards
Joffm
in this case - filter a single punch question on a single punch question - I have a different approach.
To be honest: I prefer out of the box solutions just because of my lack of JS-knowledge.
Okay, Tony's script here is short, straight forward and easy to understand.
Let me explain:
Between the two single questions (here: course Q1 and teacher Q2) I insert an equation and a multiple question M1 (both hidden).
The multiple question with exactly the same answer options and codes as the second single ("Teachers") is used to be the matrix filter.
By the equation I preset the multiple question: e.g.
{M1_T1=if((Q1=="C1") OR (Q1=="C3"),"Y","")}
{M1_T2=if((Q1=="C2") OR (Q1=="C4") OR (Q1=="C5"),"Y","")}
...
And the matrix filter of Q2 is M1.
Best reqards
Joffm
Volunteers are not paid.
Not because they are worthless, but because they are priceless
Attachments:
The topic has been locked.
- AdinaNica1
- Offline
- New Member
Less
More
- Posts: 13
- Thank you received: 0
6 years 5 months ago #160372
by AdinaNica1
Replied by AdinaNica1 on topic Answer options based on previous question
Super! The demo is working (and this didn't happen to the previous demos)!
Can I use this script if the second question is multiple (I need to include a list of brands here)?
Adina
Can I use this script if the second question is multiple (I need to include a list of brands here)?
Adina
The topic has been locked.
- Joffm
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 12881
- Thank you received: 3965
6 years 5 months ago #160375
by Joffm
Volunteers are not paid.
Not because they are worthless, but because they are priceless
Replied by Joffm on topic Answer options based on previous question
Adina,
did you try?
Of course. The "trick" is the array filter. Everywhere you can use an array filter it will work.
Joffm
did you try?
Of course. The "trick" is the array filter. Everywhere you can use an array filter it will work.
Joffm
Volunteers are not paid.
Not because they are worthless, but because they are priceless
Attachments:
The topic has been locked.
- Joffm
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 12881
- Thank you received: 3965
6 years 5 months ago #160381
by Joffm
Volunteers are not paid.
Not because they are worthless, but because they are priceless
Replied by Joffm on topic Answer options based on previous question
Well, not to be confused.
I only said "you can do that". But of course you do not
In reality you use just subquestion relevance if the second question is multiple.
The whole workaround is only necessary, because there is no subquestion relevance in single punch questions (there are no subquestions)
Joffm
I only said "you can do that". But of course you do not
In reality you use just subquestion relevance if the second question is multiple.
The whole workaround is only necessary, because there is no subquestion relevance in single punch questions (there are no subquestions)
Joffm
Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.
- AdinaNica1
- Offline
- New Member
Less
More
- Posts: 13
- Thank you received: 0
6 years 5 months ago #160383
by AdinaNica1
Replied by AdinaNica1 on topic Answer options based on previous question
Excellent! I will try now!! Many many thanks!!!
The topic has been locked.
- AdinaNica1
- Offline
- New Member
Less
More
- Posts: 13
- Thank you received: 0
6 years 5 months ago #160386
by AdinaNica1
Replied by AdinaNica1 on topic Answer options based on previous question
Hi, Joff!
What I have to do:
Q1 (single) City
Q2 (multiple) Brands ever used, each city has a list of brands that are present
BUC : 'CL1,CL3,CL5',
BV : 'CL1,CL2,CL3,CL4',
IS : 'CL2,CL3,CL4,CL5',
TM : 'CL1,CL4,CL6'
Q3 (single) Brand used most often - show only the brands that are ticked in Q2
I tried to implement the scripts in my survey and the version that finally worked for me was:
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
// The Q2 options corresponding to the Q1 options
ListaClinici = {
BUC : 'CL1,CL3,CL5',
BV : 'CL1,CL2,CL3,CL4',
IS : 'CL2,CL3,CL4,CL5',
TM : 'CL1,CL4,CL6'
};
// Identify this question
var thisQuestion = $('#question{QID}')
// The checked option in Q1
var q1Ans = '{Q1}';
// Hide all of the clinics
$('.answer-item', thisQuestion).hide();
// Now show the appropriate ones
$(ListaClinici[q1Ans].split(',')).each(function(i){
$('.answer-item[id$="'+{QID}+this+'"]').show();
});
// Uncheck all hidden radios (in case page has been seen before)
$('.answer-item:hidden input.radio', thisQuestion).prop('checked', false);
});
</script>
Now, I have the following situation:
In Q3, i need to show only the brands that are ticked in Q2 (multiple answer). I put the array filter and it doesn't work properly, it doesn't show the correct list of chosen answers in Q2. For some cities it works, for some other it doesn't.
I also attach the survey, I think it is something I miss here! Can you have a look on the syntax?
Many many thanks! [attachment=11079]Clinics test adina.lss
What I have to do:
Q1 (single) City
Q2 (multiple) Brands ever used, each city has a list of brands that are present
BUC : 'CL1,CL3,CL5',
BV : 'CL1,CL2,CL3,CL4',
IS : 'CL2,CL3,CL4,CL5',
TM : 'CL1,CL4,CL6'
Q3 (single) Brand used most often - show only the brands that are ticked in Q2
I tried to implement the scripts in my survey and the version that finally worked for me was:
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
// The Q2 options corresponding to the Q1 options
ListaClinici = {
BUC : 'CL1,CL3,CL5',
BV : 'CL1,CL2,CL3,CL4',
IS : 'CL2,CL3,CL4,CL5',
TM : 'CL1,CL4,CL6'
};
// Identify this question
var thisQuestion = $('#question{QID}')
// The checked option in Q1
var q1Ans = '{Q1}';
// Hide all of the clinics
$('.answer-item', thisQuestion).hide();
// Now show the appropriate ones
$(ListaClinici[q1Ans].split(',')).each(function(i){
$('.answer-item[id$="'+{QID}+this+'"]').show();
});
// Uncheck all hidden radios (in case page has been seen before)
$('.answer-item:hidden input.radio', thisQuestion).prop('checked', false);
});
</script>
Now, I have the following situation:
In Q3, i need to show only the brands that are ticked in Q2 (multiple answer). I put the array filter and it doesn't work properly, it doesn't show the correct list of chosen answers in Q2. For some cities it works, for some other it doesn't.
I also attach the survey, I think it is something I miss here! Can you have a look on the syntax?
Many many thanks! [attachment=11079]Clinics test adina.lss
Attachments:
The topic has been locked.
- AdinaNica1
- Offline
- New Member
Less
More
- Posts: 13
- Thank you received: 0
6 years 5 months ago #160387
by AdinaNica1
Replied by AdinaNica1 on topic Answer options based on previous question
In the meantime I noticed that only if I tick CL3 in Q2 I get the error
The topic has been locked.