Welcome to the LimeSurvey Community Forum

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

Multiple choice scale instead of dropdown list in array

  • indrek
  • indrek's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
1 year 3 months ago #239331 by indrek
Please help us help you and fill where relevant:
Your LimeSurvey version: LimeSurvey Community Edition 5.4.2
Own server or LimeSurvey hosting: own server
Survey theme/template: university's custom theme
==================
There are forum examples of how to change text fields to dropdown lists or to radio buttons in an array. Is it possible to change such an array so that one of the dropdown lists is presented as multiple choices? What I'm looking for is a way to present several mutually non-exclusive answer options per cell in an array. A dropdown list or radio buttons would work for mutually exclusive answer options, but that's not what I'm looking for here.

Please Log in to join the conversation.

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 year 3 months ago #239339 by tpartner
I would prefer to use check-boxes as respondents are more familiar with them and select elements with the "multiple" attribute are displayed expanded by most browsers anyway.

Having said that, this script in the source of an array-texts will insert drop-downs with the "multiple" attribute into the first column.

Code:
<script type="text/javascript" data-author="Tony Partner">
 
  $(document).on('ready pjax:scriptcomplete',function(){
 
    var thisQuestion = $('#question{QID}');
     
    if($('.inserted-select', thisQuestion).length == 0) {
     
      // Column-specific classes
      $('tr.subquestion-list', thisQuestion).each(function(i) {
        $('th, td', this).each(function(i) {
          $(this).addClass('column-'+i);
        });
      });
       
      // Insert selects into column 1
      $('.answer-item.column-1', thisQuestion).addClass('with-select').append('<select class="inserted-select form-control list-question-select" multiple>\
      <option value="" selected>Please choose...</option>\
      <option value="option1">Option 1</option>\
      <option value="option2">Option 2</option>\
      <option value="option3">Option 3</option>\
      <option value="option4">Option 4</option>\
      </select>');
       
      // Listeners
      $('.inserted-select', thisQuestion).on('change', function(i) {
        if($(this).val() != '') {
          console.log($(this).val());
          $(this).closest('.answer-item').find('input:text').val($(this).val()).trigger('change');
        }
        else {
          $(this).closest('.answer-item').find('input:text').val('').trigger('change');
        }
      });
       
      // Returning to page
      $('.with-select input:text', thisQuestion).each(function(i) {
        var thisCell = $(this).closest('.answer-item');
        var inputText = $.trim($(this).val());
        if(inputText != '') {
          $('select.inserted-select option', thisCell).removeAttr('selected');
          var inputValues = inputText.split(',');
          $.each(inputValues, function(i, val) {
            $('select.inserted-select option[value="'+val+'"]', thisCell).attr('selected', true);
          });
        }
      });
       
      // Clean-up styles
      $('select.inserted-select', thisQuestion).css({
        'max-width': '100%'
      });
      $('.with-select input:text', thisQuestion).css({
        'position': 'absolute',
        'left': '-9999em'
      });
    }
  });
</script>



Sample survey attached:  

File Attachment:

File Name: limesurvey...9551.lss
File Size:34 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: indrek

Please Log in to join the conversation.

  • indrek
  • indrek's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
1 year 3 months ago #239350 by indrek
Thanks a lot, Tony, that's pretty much what I was trying to achieve! However, check boxes would indeed be more intuitive for the respondent as they can be used with mouse only. Is there a way to present the same answer options as check boxes?

Please Log in to join the conversation.

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 year 3 months ago #239351 by tpartner
This script will insert check-boxes into column-1 of an array question:

Code:
<script type="text/javascript" data-author="Tony Partner">
 
  $(document).on('ready pjax:scriptcomplete',function(){
 
    var thisQuestion = $('#question{QID}');
     
    if($('.inserted-select', thisQuestion).length == 0) {
     
      // Column-specific classes
      $('tr.subquestion-list', thisQuestion).each(function(i) {
        $('th, td', this).each(function(i) {
          $(this).addClass('column-'+i);
        });
      });
       
      // Insert checkboxes into column 1
      $('.answer-item.column-1', thisQuestion).each(function(i) {
        var column = 1;
        var row = i+1;
        $(this).addClass('with-multi-opt').append('<ul class="list-unstyled inserted-list">\
            <li class="question-item checkbox-item inserted-item">\
              <input type="checkbox" id="'+row+'_'+column+'_box1" value="option1">\
              <label for="'+row+'_'+column+'_box1" class="checkbox-label control-label">Option 1</label>\
            </li>\
            <li class="question-item checkbox-item  inserted-item">\
              <input type="checkbox" id="'+row+'_'+column+'_box2" value="option2">\
              <label for="'+row+'_'+column+'_box2" class="checkbox-label control-label">Option 2</label>\
            </li>\
            <li class="question-item checkbox-item  inserted-item">\
              <input type="checkbox" id="'+row+'_'+column+'_box3" value="option3">\
              <label for="'+row+'_'+column+'_box3" class="checkbox-label control-label">Option 3</label>\
            </li>\
          </ul>');
      });
       
      // Listeners
      $('.inserted-item :checkbox', thisQuestion).on('change', function(i) {
        var thisCell = $(this).closest('.answer-item');
        var values = [];
        $('.inserted-item :checkbox:checked', thisCell).each(function(i) {
          values.push($(this).val());
        });
        $('input:text', thisCell).val(values);
      });
       
      // Returning to page
      $('.with-multi-opt input:text', thisQuestion).each(function(i) {
        var thisCell = $(this).closest('.answer-item');
        var inputValues = $.trim($(this).val());
        if(inputValues != '') {
          console.log(inputValues);
          var prevValues = inputValues.split(',');
          $.each(prevValues, function(i, val) {
            $('.inserted-item :checkbox[value="'+val+'"]', thisCell).prop('checked', true);
          });
        }
      });
       
      // Clean-up styles
      $('.inserted-list', thisQuestion).css({
        'margin': '0',
      });
      $('.inserted-list li', thisQuestion).css({
        'margin': '5px 0 -5px 0',
        'padding': '0 0 0 20px',
        'text-align': 'left'
      });
      $('.with-multi-opt input:text', thisQuestion).css({
        'position': 'absolute',
        'left': '-9999em'
      });
    }
  });
</script>



Sample survey attached:  

File Attachment:

File Name: limesurvey...1(1).lss
File Size:46 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: indrek

Please Log in to join the conversation.

  • indrek
  • indrek's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
1 year 3 months ago #239371 by indrek
Awesome, many thanks!

Please Log in to join the conversation.

Lime-years ahead

Online-surveys for every purse and purpose