Welcome to the LimeSurvey Community Forum

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

Help restricting selectable multiple choice answers

  • blocka
  • blocka's Avatar Topic Author
  • Offline
  • Platinum Member
  • Platinum Member
More
6 years 4 months ago - 6 years 4 months ago #161218 by blocka
I have a multiple choice question where some answer options are mutually exclusive, and I'd like to impose logic to enforce this (ideally by hiding/showing allowed options in real-time).

Here's my question and the rules:

Please select from the items below:

1) Answer choice 1
2) Answer choice 2
3) Answer choice 3
4) Answer choice 4
5) None of the above
6) Don’t know
7) Not applicable

Rules:

A. If I select "1) answer choice 1" and/or "2) answer choice 2" and/or "3) answer choice 3" and/or "4) answer choice 4"
--> Then I would not be able to select "5) None of the above", "6) Don’t know", or "7) Not Applicable"

B. If I select "5) None of the above",
--> Then I would not be able to select 1) or 2) or 3) or 4) or 6) or 7)

C. If I select "6) Don’t know",
--> Then I would not be able to select 1) or 2) or 3) or 4) or 5) or 7)

D. If I select "7) Not applicable",
--> Then I would not be able to select 1) or 2) or 3) or 4) or 5) or 6)
Last edit: 6 years 4 months ago by blocka.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
6 years 4 months ago #161225 by tpartner
Please refer to the manual - Exclusive Options - manual.limesurvey.org/Question_type_-_Mu...xclude_all_others.29

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • blocka
  • blocka's Avatar Topic Author
  • Offline
  • Platinum Member
  • Platinum Member
More
6 years 4 months ago #161229 by blocka
Well, that makes it easy! I wonder though, is there a way to uncheck the other options -- see below, I've selected (6) Don't know, but prior to that, I chose (1) Answer choice 1 ... it would be nice if Choice (1) was deselected...

The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
6 years 4 months ago #161231 by tpartner
To do that, you will need to use JavaScript instead of the built-in exclusive feature. Something like in this post but modified to render all options placed greater than 4 exclusive.

Code:
<script type="text/javascript" charset="utf-8">    
 
  $(document).ready(function() {
 
    // Identify this question
    var thisQuestion = $('#question{QID}');
 
    // Add some classes
    $('.question-item:not(:gt(3))', thisQuestion).addClass('non-exclusive-item');
    $('.question-item:gt(3)', thisQuestion).addClass('exclusive-item');
 
    // Handle exclusive items
    $('input.checkbox', thisQuestion).on('change', function(e) {
      if($(this).is(':checked')) {
        var actionItems = $('.question-item', thisQuestion).not($(this).closest('.question-item'));
        if($(this).closest('.question-item').hasClass('non-exclusive-item')) {
          actionItems = $('.exclusive-item', thisQuestion);
        }
        actionItems.each(function(i) {
          $('input.checkbox', this).prop('checked', false).trigger('change');
          $('input:hidden', this).attr('value', '');
          $('input[type="text"]', this).val('').trigger('keyup');
        });
      }
    });
 
  });
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose