Welcome to the LimeSurvey Community Forum

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

Array filter and pre-populating a multiple choice question

  • RaCMJS
  • RaCMJS's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
8 years 11 months ago #118583 by RaCMJS
I have only now figured out how to use Array Filter and am very excited about it. However, I can't figure out how to use it if I don't have a corresponding multiple choice question. Here is what we are trying to do in our survey:

We ask the respondent:
Are your parents still living?
- Both
- Only Mother
- Only Father
- Neither

Are your spouse's parents still living?
- Both
- Only Mother
- Only Father
- Neither

These questions appear early in the survey and are each in a separate group and can't be combined. I prefer not using check boxes because we end up with too much missing and confusing data.

Later on I want to have the following array, but only show the people that are alive based on the previous questions.

Thinking about your children how close are they to...
Your mother
Your father
Your spouse's mother
Your spouse's father

Short of having multiple arrays that fit each possible scenario and use branching to control what shows up how would I handle this? Is there a way to have a hidden multiple choice question that would get ticked based on the responses to the original 2 questions?

Many thanks.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
8 years 11 months ago #118608 by tpartner
I think you will need to place a hidden multiple-choice question in the same group as "Thinking about your children..." and use JavaScript to toggle the checkboxes depending on the results of the first two questions (Expression manager can be used to pipe in the results from those questions). Array filtering can do the rest.

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: RaCMJS
The topic has been locked.
  • RaCMJS
  • RaCMJS's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
8 years 11 months ago #118613 by RaCMJS
Ok, this is helpful. However I have NO IDEA how to write JavaScript.... :(
I tried this but it's obviously wrong. Help?

{if (resppar==1 or resppar==2) { document.getElementById('granparnets_mgm').checked = true;}
{if (resppar==1 or resppar==3) { document.getElementById('granparnets_mgf').checked = true;}
{if (sppar==1 or sppar==2) { document.getElementById('granparnets_pgm').checked = true;}
{if (sppar==1 or sppar==3) { document.getElementById('granparnets_pgf').checked = true;}
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
8 years 11 months ago #118657 by tpartner
If, for example, you have:
- Question codes q1, q2, q3 for your existing questions
- Question code qHidden for the new multiple-choice question
- Answer/sub-question codes 1, 2, 3, 4 for ALL questions

Placing a script like this in the source of the new multiple-choice will hide it and toggle the check-boxes according to the q1 and q2 answers. If you set q3 to be filtered by qHidden, the appropriate rows will be displayed.

Code:
<script type="text/javascript" charset="utf-8">  
  $(document).ready(function() {
 
    // Identify this question
    var thisQuestion = $('#question{QID}');
 
    // Hide this question
    thisQuestion.hide();
 
    // Get the previous answers
    var q1Answer = {q1};
    var q2Answer = {q2};
 
    // Reset the checkboxes
    $('input[type="checkbox"]', thisQuestion).prop('checked', false);
    $('li.question-item input[type="hidden"]', thisQuestion).val('');
 
    // Toggle the appropriate checkboxes
    switch(q1Answer) {
      case 1:
        $('input[type="checkbox"]:eq(0)', thisQuestion).prop('checked', true);
        $('input[type="checkbox"]:eq(0)', thisQuestion).nextAll('input[type="hidden"]').val('Y');
        $('input[type="checkbox"]:eq(1)', thisQuestion).prop('checked', true);
        $('input[type="checkbox"]:eq(1)', thisQuestion).nextAll('input[type="hidden"]').val('Y');
        break;
      case 2:
        $('input[type="checkbox"]:eq(0)', thisQuestion).prop('checked', true);
        $('input[type="checkbox"]:eq(0)', thisQuestion).nextAll('input[type="hidden"]').val('Y');
        break;
      case 3:
        $('input[type="checkbox"]:eq(1)', thisQuestion).prop('checked', true);
        $('input[type="checkbox"]:eq(1)', thisQuestion).nextAll('input[type="hidden"]').val('Y');
        break;
    }
    switch(q2Answer) {
      case 1:
        $('input[type="checkbox"]:eq(2)', thisQuestion).prop('checked', true);
        $('input[type="checkbox"]:eq(2)', thisQuestion).nextAll('input[type="hidden"]').val('Y');
        $('input[type="checkbox"]:eq(3)', thisQuestion).prop('checked', true);
        $('input[type="checkbox"]:eq(3)', thisQuestion).nextAll('input[type="hidden"]').val('Y');
        break;
      case 2:
        $('input[type="checkbox"]:eq(2)', thisQuestion).prop('checked', true);
        $('input[type="checkbox"]:eq(2)', thisQuestion).nextAll('input[type="hidden"]').val('Y');
        break;
      case 3:
        $('input[type="checkbox"]:eq(3)', thisQuestion).prop('checked', true);
        $('input[type="checkbox"]:eq(3)', thisQuestion).nextAll('input[type="hidden"]').val('Y');
        break;
    }
 
    // Fire the checkconditions() function for filtering
    $('input[type="checkbox"]', thisQuestion).each(function(i) {
      checkconditions(this.value, this.name, this.type);
    });
  });
</script>

Here's a small sample survey:

File Attachment:

File Name: limesurvey...21-2.lss
File Size:25 KB

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: RaCMJS
The topic has been locked.
  • RaCMJS
  • RaCMJS's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
8 years 11 months ago #118663 by RaCMJS
THANK YOU, THANK YOU, THANK YOU!
This is AWESOME! You totally made my day!
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose