Welcome to the LimeSurvey Community Forum

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

Array filter of the answers of 2 questions

  • limeuser2014
  • limeuser2014's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
9 years 7 months ago - 9 years 7 months ago #111368 by limeuser2014
Array filter of the answers of 2 questions was created by limeuser2014
I have 3 questions in my survey. The type of these questions is multiple choice.

Q1. What animals do you know?
1. A cat
2. A dog
3. A monkey
4. A pig
5. An ekhtiander
6. A fox

Q2. Do you know animals such as..? [Array exclusion filter of Q1]
1. A cat
2. A dog
3. A monkey
4. A pig
5. An ekhtiander
6. A fox

Q3. What animals have you ever had?
[The available variants of the answers must be from Q1 and Q2 together]

I would like to variants of answers will be all answers of Q1 and Q2 together.
Array filter in Q3 allows me to choice one question only.

How can I do it?

I think maybe I should remove array exclusion filter in Q2 and insert JavaScript code to Q2 that the answers of Q1 were copied to Q2 and I should set array filter to Q3 value Q2. But I can't write this JavaScript code.

Do you have any solutions?

P.S. I am sorry for my English.
Last edit: 9 years 7 months ago by limeuser2014.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
9 years 7 months ago #111436 by tpartner
Replied by tpartner on topic Array filter of the answers of 2 questions
The array filter works like this:

1) If the question codes in all filter questions are the same, the option must be selected in ALL filter questions to appear in the filtered array.

2) If the question codes in the filter questions unique, the option must be selected in ONE filter questions to appear in the filtered array (Q3).

So, a simple solution for you may be to give the options in Q1 and Q2 unique codes and then "double up" the array rows with matching unique codes. Filter Q3 with both Q1 and Q2.

Of course, you will need to handle these doubled sub-questions in post survey analysis.

Q1. What animals do you know?
1. A cat
2. A dog
3. A monkey
4. A pig
5. An ekhtiander
6. A fox

Q2. Do you know animals such as..? [Array exclusion filter of Q1]
7. A cat
8. A dog
9. A monkey
10. A pig
11. An ekhtiander
12. A fox

Q3. What animals have you ever had?
1. A cat
2. A dog
3. A monkey
4. A pig
5. An ekhtiander
6. A fox
7. A cat
8. A dog
9. A monkey
10. A pig
11. An ekhtiander
12. A fox


.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • limeuser2014
  • limeuser2014's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
9 years 7 months ago #111453 by limeuser2014
Replied by limeuser2014 on topic Array filter of the answers of 2 questions
tpartner, thank you for reply.

I done it and Q3 works as I wanted.

But now array exclusion filter on Q2 (excluding Q1 answers) don't work.

How can I hide Q1 answers in Q2?
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
9 years 7 months ago #111455 by tpartner
Replied by tpartner on topic Array filter of the answers of 2 questions
Oh, yeah, I zoned on that. You'll need to use JavaScript to handle that.

Assuming you are using LS 2.05 and Q1 and Q2 are on the same page and are sequential, adding this to the source of Q1 should do the trick.

Code:
<script type="text/javascript" charset="utf-8">  
    $(document).ready(function(){
 
    // Identify the questions
    var q1= $('#question{QID}');
    var q2 = $(q1).nextAll('.multiple-opt:eq(0)');
 
    // Initial states
    $('input.checkbox:checked', q1).each(function(i){
      var thisIndex = $(this).closest('.question-item').index();
      var q2Item = $('.question-item:eq('+thisIndex+')', q2);
      $(q2Item).hide();
    });
 
    // Listener on Q1
    $('input.checkbox', q1).change(function(event){
      var thisIndex = $(this).closest('.question-item').index();
      var q2Item = $('.question-item:eq('+thisIndex+')', q2);
      var q2Input = $('input.checkbox', q2Item);
      if($(this).is(':checked')) {
        $(q2Input).prop('checked', false);
        $('input[type="hidden"]', q2Item).val('');
        $(q2Item).hide();
        checkconditions($(q2Input).attr('value'), $(q2Input).attr('name'), $(q2Input).attr('type'))
      }
      else {
        $(q2Item).show();
      }
    });
  });
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: limeuser2014
The topic has been locked.
  • limeuser2014
  • limeuser2014's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
9 years 7 months ago #111458 by limeuser2014
Replied by limeuser2014 on topic Array filter of the answers of 2 questions
Tony, thank you for you help.

I attached the working example for someone.
The topic has been locked.
More
9 years 7 months ago #111626 by srocky
Replied by srocky on topic Array filter of the answers of 2 questions
Can I ask one more question about this solution?

Is it possible to set by JavaScript that one of options (ex. "non of above") will always be visible?

I have two questions Q1 and Q2, and every of them have exclusion option: Non of above. But when someone check this option in Q1 it disappear in Q2.

Please help.

(sorry for my English)
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
9 years 7 months ago #111632 by tpartner
Replied by tpartner on topic Array filter of the answers of 2 questions
Try changing this line:
Code:
if($(this).is(':checked')) {
To this:
Code:
if($(this).is(':checked') &amp;&amp; (thisIndex+1) != $('.question-item', q1).length) {

That should prevent filtering on the last items.


.

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
9 years 7 months ago #111635 by srocky
Replied by srocky on topic Array filter of the answers of 2 questions
It works :) Thank you a lot!
The topic has been locked.
More
3 years 10 months ago - 3 years 10 months ago #197572 by slarson55
Replied by slarson55 on topic Array filter of the answers of 2 questions
Deleted.
Last edit: 3 years 10 months ago by slarson55. Reason: Realized this was not a totally related thread.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose