Welcome to the LimeSurvey Community Forum

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

uncheck some of the multiple choice answers

  • KompetenzZ
  • KompetenzZ's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
5 years 4 months ago - 5 years 4 months ago #178137 by KompetenzZ
Hi community,

I found this javascript for multiple response questions to automatically uncheck other options as options are checked: www.limesurvey.org/forum/can-i-do-this-w...vious-answers#176395
Code:
<script type="text/javascript" charset="utf-8">
 
  $(document).on('ready pjax:scriptcomplete',function(){
 
    // Identify this question
    var thisQuestion = $('#question{QID}');
 
    // Listener on the checkboxes
    $('input:checkbox', thisQuestion).on('change', function(e) {
      if($(this).is(':checked')) {
        $('input:checkbox', thisQuestion).not(this).prop('checked', false).trigger('change');
      }
    });
  });
</script>

Could someone please help me that this javascript only applies for some of my subquestions:


1 (subquestion - multiple choice)
2 (subquestion - multiple choice)
3 (subquestion - multiple choice)
4 (subquestion - multiple choice)
5 (subquestion - multiple choice)

Bold subquestions: When "1" is already checked and you check "3", "1" should uncheck, vice versa. But this should not apply to (unbold) "2", "4" and "5" (they can be checked (together) with "1" or "3". (For me it does not matter if they uncheck or not when two bold answers are checked).

That means "1" and "3" are only exclusive for each other, but not with "2", "4" and "5".

LS Version 3.14

Cheers kompetenzz
Last edit: 5 years 4 months ago by KompetenzZ.
The topic has been locked.
  • KompetenzZ
  • KompetenzZ's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
5 years 4 months ago - 5 years 4 months ago #178301 by KompetenzZ
Replied by KompetenzZ on topic uncheck some of the multiple choice answers
Just for documentation: I have found a way to achieve what I was looking for in the first post. Since I have almost no experience with java script, it was pretty exhausting to tinker around with the two classes in javascript. This is the code where only some of the multiple choice answers are mutually exclusive:

Code:
<script type="text/javascript" charset="utf-8">    
 
  $(document).ready(function() {
 
    // Identify this question
    var thisQuestion = $('#question{QID}');
 
    // Add some classes
    var sqCount = $('.question-item', thisQuestion).length;
 
                             $('.question-item:eq('+(sqCount-22)+')', thisQuestion).addClass('non-exclusive-item');  // first item
                             $('.question-item:eq('+(sqCount-21)+')', thisQuestion).addClass('non-exclusive-item');  // 2nd item
                             $('.question-item:eq('+(sqCount-20)+')', thisQuestion).addClass('non-exclusive-item');
 
                             $('.question-item:eq('+(sqCount-18)+')', thisQuestion).addClass('non-exclusive-item');
                             $('.question-item:eq('+(sqCount-17)+')', thisQuestion).addClass('non-exclusive-item');
                             $('.question-item:eq('+(sqCount-16)+')', thisQuestion).addClass('non-exclusive-item');
 
                             $('.question-item:eq('+(sqCount-14)+')', thisQuestion).addClass('non-exclusive-item');
                             $('.question-item:eq('+(sqCount-13)+')', thisQuestion).addClass('non-exclusive-item');
                             $('.question-item:eq('+(sqCount-12)+')', thisQuestion).addClass('non-exclusive-item');
 
                             $('.question-item:eq('+(sqCount-10)+')', thisQuestion).addClass('non-exclusive-item');
                             $('.question-item:eq('+(sqCount-9)+')', thisQuestion).addClass('non-exclusive-item');
                             $('.question-item:eq('+(sqCount-8)+')', thisQuestion).addClass('non-exclusive-item');
 
                             $('.question-item:eq('+(sqCount-6)+')', thisQuestion).addClass('non-exclusive-item');
                             $('.question-item:eq('+(sqCount-5)+')', thisQuestion).addClass('non-exclusive-item');
 
                             $('.question-item:eq('+(sqCount-3)+')', thisQuestion).addClass('non-exclusive-item');
                             $('.question-item:eq('+(sqCount-2)+')', thisQuestion).addClass('non-exclusive-item');  // // 2nd last item
                             $('.question-item:eq('+(sqCount-1)+')', thisQuestion).addClass('non-exclusive-item');  // Last item
 
 
    // Handle exclusive items
    $('input[type="checkbox"]', thisQuestion).on('change', function(e) {
      if($(this).is(':checked') ) {
        var thisItem = $(this).closest('.question-item');
        var actionItems = $('.question-item', thisQuestion).not($(thisItem));
        if($(thisItem).hasClass('non-exclusive-item')) {
          actionItems = $('.exclusive-item', thisQuestion);
        }
        actionItems.each(function(i) {
          $('input[type="checkbox"]', this).prop('checked', false).trigger('change');
          $('input:hidden', this).attr('value', '');
          $('input[type="text"]', this).val('').trigger('keyup');
        });
      }
    });
    $('input[type="text"]', thisQuestion).on('keyup change', function(e) {
      var thisInput = $(this);
      setTimeout(function() {
        $(thisInput).closest('.question-item').find('input[type="checkbox"]').trigger('change');
      }, 200);
    });
 
  });
</script>
I took the javascript from here and modified it my needs: www.limesurvey.org/forum/can-i-do-this-w...oxes?start=15#171315

Therefore, there are some parts "junk code" left (in genetics they would be called introns ;)), however, the javascript works.
An example, which combines secondary answer options ( manual.limesurvey.org/Workarounds:_Manip..._options.22_question ) together with choosing only one primary answer and one secondary answer is attached (theme needs to be imported for secondary answer options). For me this workaround was very helpful.

File Attachment:

File Name: limesurvey...8874.lss
File Size:32 KB

File Attachment:

File Name: extends_fruity1.zip
File Size:132 KB

Cheers kompetenzz
Last edit: 5 years 4 months ago by KompetenzZ.
The following user(s) said Thank You: DenisChenu
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose