Welcome to the LimeSurvey Community Forum

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

Add condition for certain answer in the array question type ?

  • sherip008
  • sherip008's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 2 months ago #221764 by sherip008
I made an array (numbers) question to make it a multiple choice question with a time sequence. However, I would also want to add additional condition as follow:
 
Condition: If they choose "not at all", no other answers will be selected ( greyed out if "Not at all" is selected). Can I possibly do this with LimeSurvey ?

Cheers !
 

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 2 months ago #221770 by Joffm
Enter this in the question text (source code mode)
Code:
<script type="text/javascript" charset="utf-8">
    $(document).ready(function() {
        // Call the exclude function using question ID
        excludeOpt({QID});
    });
 
    // A function to make the last option in each array row exclusive
    function excludeOpt (qID) {
 
        var thisQuestion = $('#question'+qID)
 
        // Add some classes to the checkbox cells
        $('td.checkbox-item', thisQuestion).addClass('normal-item');
        $('tr.subquestion-list', thisQuestion).each(function(i) {
        $('.normal-item:last', this).removeClass('normal-item').addClass('exlusive-item')
        });
 
        // A listener on the checkboxes
        $('input[type="checkbox"]', thisQuestion).on('change', function (event) {
            handleExclusive($(this).closest('td'));
        });
 
        function handleExclusive(thisCell) {
 
            var thisRow = $(thisCell).closest('tr');
 
            // Uncheck the appropriate boxes in a row
            if ($(thisCell).hasClass('normal-item')) {
                $('.exlusive-item input[type="checkbox"]', thisRow).prop('checked', false);
            }
            else {
                $('.normal-item input[type="checkbox"]', thisRow).prop('checked', false);
            }
 
            // Check conditions (relevance)
            $('td.checkbox-item', thisRow).each(function(i) {
                var thisValue = '';
                if($('input[type="checkbox"]', this).is(':checked')) {
                    thisValue = 1;
                }
                var thisSGQA = $('input[type="checkbox"]', this).attr('id').replace(/cbox_/, '');
 
                $('input[type="hidden"]', this).attr('value', thisValue);
                fixnum_checkconditions(thisValue, thisSGQA, 'hidden');
            });
        }
    }
</script>

Joffm
 

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The following user(s) said Thank You: sherip008

Please Log in to join the conversation.

  • sherip008
  • sherip008's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 2 months ago #221775 by sherip008
Thank you. Just did some extra research and found that my university probably turned on "Filter HTML for XSS". When I paste the javascript code into the question and save it, the code auto disappear once saved.
Appreciate the help !

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 2 months ago #221776 by Joffm
Bad luck.

But anyway, you may validate that this option is exclusive.

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

More
2 years 10 months ago #226474 by eniisula
Hi Joffm,

How could this code be altered so I could have the last two options exclusive not only the last one?

Thank you in advance!

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 10 months ago #226477 by Joffm
Hi,
1. remove the ":last" selector and use two lines with a ":nth-last-child(x)" selector
[url] www.w3schools.com/jquery/sel_nthlastchild.asp [/url]

2. use two classes: "exclusive-item1" and "exclusive-item2" to be able to uncheck "Exclusive1" if "Exclusive2" is selected.

3. Change this part with the addition
Code:
            // Uncheck the appropriate boxes in a row
            if ($(thisCell).hasClass('normal-item')) {
                $('.exclusive-item1 input[type="checkbox"]', thisRow).prop('checked', false);
                $('.exclusive-item2 input[type="checkbox"]', thisRow).prop('checked', false);
            }
            else {
                $('.normal-item input[type="checkbox"]', thisRow).prop('checked', false);
            }
            if ($(thisCell).hasClass('exclusive-item1')) {
                $('.exclusive-item2 input[type="checkbox"]', thisRow).prop('checked', false);
            }  
            if ($(thisCell).hasClass('exclusive-item2')) {
                $('.exclusive-item1 input[type="checkbox"]', thisRow).prop('checked', false);
            }  

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The following user(s) said Thank You: LISHANGQI, eniisula

Please Log in to join the conversation.

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 10 months ago #226479 by tpartner
That is a quite old script. Here is an updated version. Adjust the exclusiveNumber variable to define how many columns from the end of each row you want exclusive.

Code:
<script type="text/javascript" data-author="Tony Partner">  
  $(document).on('ready pjax:scriptcomplete',function(){
 
    var exclusiveNumber = 2;
 
    var thisQuestion = $('#question{QID}');
 
        // Add some classes to the checkbox cells
        $('td.checkbox-item', thisQuestion).addClass('normal-item');
        $('tr.subquestion-list', thisQuestion).each(function(i) {
          $('td.checkbox-item', this).slice('-'+exclusiveNumber+'').removeClass('normal-item').addClass('exclusive-item');
        });
 
    // Listener on the checkboxes
    $('td.checkbox-item :checkbox', thisQuestion).on('change', function(e){
 
      if($(this).is(':checked')) {
        var thisRow = $(this).closest('tr');
        var thisCell = $(this).closest('td');
 
        // Identify checkbox items to uncheck
        var excludedItems = $('td.exclusive-item', thisRow);
        if ($(thisCell).hasClass('exclusive-item')) {
          excludedItems = $('td.checkbox-item', thisRow).not(thisCell);
        }
 
        // Uncheck the boxes        
        $(excludedItems).each(function(i) {
          $('input[type="hidden"]', this).val('');
          $(':checkbox', this).prop('checked', false).trigger('change');
        });
      }
    });
  });
</script>

Sample survey attached: 

File Attachment:

File Name: limesurvey...9128.lss
File Size:37 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: LISHANGQI, eniisula, mapage

Please Log in to join the conversation.

Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose