Welcome to the LimeSurvey Community Forum

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

Array numbers with checkbox and an exclusive option

More
5 years 11 months ago - 5 years 11 months ago #168866 by stephanied
My apologies. Are you more so looking for the survey structure (.lss)?
Last edit: 5 years 11 months ago by stephanied.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 11 months ago #168915 by tpartner
This script will render the last row of an array-checkboxes as exclusive. Additionally, when a check-box in the last row is checked, all other check-boxes in that column are disabled.

Code:
<script type="text/javascript" charset="utf-8">
  $(document).ready(function() {
 
    var thisQuestion = $('#question{QID}')
 
    // Add column-specific classes
    $('tr.subquestion-list', thisQuestion).each(function(i) {
      $('th, td', this).each(function(i) {
        $(this).addClass('column-'+i).attr('data-column', i);
      });
    });
 
    // Add some classes to the checkbox cells
    $('td.checkbox-item', thisQuestion).addClass('normal-opt');
    $('tr.subquestion-list:last td.checkbox-item', thisQuestion).removeClass('normal-opt').addClass('exclusive-opt');
 
    // A listener on the checkboxes
    $('td.checkbox-item input[type="checkbox"]', thisQuestion).change(function (event) {
      handleExclusive($(this).closest('td'));
    });
 
    function handleExclusive(thisCell) {
 
      var thisColumn = $(thisCell).attr('data-column');
      var thisCheckBox = $('input[type="checkbox"]', thisCell);
 
      // Uncheck the appropriate boxes in a row
      $('.checkbox-item[data-column="'+thisColumn+'"] input[type="checkbox"]').prop('disabled', false);
      if ($(thisCell).hasClass('normal-opt') &amp;&amp; $(thisCheckBox).is(':checked')) {
        $('.exclusive-opt[data-column="'+thisColumn+'"] input:hidden').val('');
        $('.exclusive-opt[data-column="'+thisColumn+'"] input[type="checkbox"]').prop('checked', false);
      }
      else if ($(thisCheckBox).is(':checked')) {
        $('.normal-opt[data-column="'+thisColumn+'"] input:hidden').val('');
        $('.normal-opt[data-column="'+thisColumn+'"] input[type="checkbox"]').prop('checked', false).prop('disabled', true);
      }
 
      // Check conditions (relevance)
      $('td.checkbox-item[data-column="'+thisColumn+'"]').each(function(i) {
 
        var thisValue = '';
        var itemCheckBox = $('input[type="checkbox"]', this);
        var thisSGQA = $(itemCheckBox).attr('id').replace(/cbox_/, '');
        var aelt = $('#answer'+thisSGQA);
 
        fixnum_checkconditions($(aelt).val(), thisSGQA, 'hidden');
        checkconditions($(itemCheckBox).attr('value'), $(itemCheckBox).attr('name'), $(itemCheckBox).attr('type'))
      });
    }
  });
</script>



Here is your test survey back with that script in the source of the array:

File Attachment:

File Name: limesurvey...2581.lss
File Size:19 KB

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