Welcome to the LimeSurvey Community Forum

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

Is it possible to make single choice answer options conditional

  • byan
  • byan's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
2 years 3 months ago #233011 by byan
Please help us help you and fill where relevant:
Your LimeSurvey version: Version 5.4.7
Own server or LimeSurvey hosting:  LimeSurvey Cloud
Survey theme/template:
==================
Dear all,

I need to add a single choice question with a list of dates (e.g., 9/30/22,  10/07/22, 10/14/22, 10/21/22, ..., 05/26/23). However, I only want to show the dates that are before today. So in the example of today, all dates after 10/14/22 will not be shown and next week only 10/21/22 will appear. Is it possible to do this? Thanks!
 

Please Log in to join the conversation.

  • byan
  • byan's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
2 years 3 months ago #233012 by byan
Found out that I could use Array filter when the singe question type is List (radio). For single question type of List (dropdown), however, Array filter is not available.

The problem with my situation is that the list will be very long when radio button is used.

Please Log in to join the conversation.

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 3 months ago #233037 by tpartner
Assuming you can place the filter question on a previous page, you can place this script in the source of a list-radio question.

It will:
- Hide the radio list.
- Insert a drop-down containing only the relevant radio items.
- Click the corresponding radio when the drop-down is changed.

Code:
<script type="text/javascript" data-author="Tony Partner">
 
  $(document).on('ready pjax:scriptcomplete',function(){
 
    // Identify this question
    var qID = '{QID}';
    var thisQuestion = $('#question'+qID);
 
    // Hide the radios
    $('.answers-list', thisQuestion).hide();
 
    // Need a small delay to allow EM to set the radio relevance    
    setTimeout(function() {
 
      // Insert the drop-down
      $('.answers-list', thisQuestion).after('<div class="form-inline">\
          <div class="ls-answers answer-item dropdown-item  form-group">\
            <select class="form-control list-question-select inserted-select">\
              <option value="">Please choose...</option>\
            </select>\
          </div>\
        </div>');
 
      // Insert the list options
      $('.radio-item:not(.ls-irrelevant) :radio', thisQuestion).each(function(i) {
        var thisVal = $(this).val();
        var thisLabel = $(this).next('label').html();
        $('.inserted-select', thisQuestion).append('<option value="'+thisVal+'">'+thisLabel+'</option>');
      });
 
      // Listener on the drop-down
      $('.inserted-select', thisQuestion).on('change', function(e) {
        var thisVal = $(this).val();
        var checkedRadio = $('.radio-item :radio[id$="'+qID+thisVal+'"]', thisQuestion);
 
        // Click the corresponding radio 
        $(checkedRadio).trigger('click');
 
        // Fire ExpressionScript
        checkconditions($(checkedRadio).attr('value'), $(checkedRadio).attr('name'), 'radio');
      });
 
      // Returning to the page
      if($('.radio-item :radio:checked', thisQuestion).length > 0) {
        var questionVal = $('.radio-item :radio:checked:eq(0)', thisQuestion).val();
        $('.inserted-select', thisQuestion).val(questionVal);
      }
    }, 500);
 
    });
</script>

Sample survey attached: 

File Attachment:

File Name: limesurvey...4(1).lss
File Size:64 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

  • byan
  • byan's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
2 years 3 months ago #233041 by byan
This is beautiful! Thank you!

Please Log in to join the conversation.

Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose