Welcome to the LimeSurvey Community Forum

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

Array filter between Scale 1 and Scale 2 in Dual Matrix

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
6 years 1 month ago #164955 by tpartner
Something like this will give a default answer of "A4" in the scale-1 drop-downs:

Code:
// Default answers in Scale 1
var defaultAnswer = 'A4';
$('select[id$="_0"]', thisQuestion).each(function(i) {
  if($(this).val() == '') {
    $(this).val(defaultAnswer);
  }
});

So, the whole script would look like this:

Code:
<script type="text/javascript" charset="utf-8">
  $(document).on('ready pjax:scriptcomplete',function(){
 
    // Identify this question
    var thisQuestion = $('#question{QID}');
 
    // Default answers in Scale 1
    var defaultAnswer = 'A4';
    $('select[id$="_0"]', thisQuestion).each(function(i) {
      if($(this).val() == '') {
        $(this).val(defaultAnswer);
      }
    });
 
 
    // Identify the "exclusive" answer(s) in scale one
    // Multiple answers separated by commas are possible
    var exclusiveValues = ['A4','A5'];
 
    // Hide the "N/A" option in scale 2
    // Set this to false if you want that option always visible
    var hideNA = true;
 
    // Identify the "N/A" value in scale 2
    var naValue = $('select[id$="_1"]:eq(0) option:last', thisQuestion).attr('value');
 
    // A function to handle the drop-down behaviour
    function handleSelects(thisSelect) {
      var selectVal = $(thisSelect).val();
      var thisRow = $(thisSelect).closest('tr');
      var exclusive = false;
      $.each(exclusiveValues, function(i, val) {
        if(selectVal == val) {
          exclusive = true;
        }
      });
      if(exclusive == true) {
        if(hideNA == true) {
          $('select[id$="_1"] option[value="'+naValue+'"]', thisRow).toggleOption(true);
        }        
        $('select[id$="_1"]', thisRow).val(naValue).prop('disabled', true);
      }
      else {
        $('select[id$="_1"]', thisRow).prop('disabled', false);
        if(hideNA == true) {
          $('select[id$="_1"] option[value="'+naValue+'"]', thisRow).toggleOption(false);
        }        
      }
    }
 
    // Listener on the drop-downs
    $('select[id$="_0"]', thisQuestion).on('change', function(e) {
      handleSelects($(this));
    });
 
    // Initial states
    $('select[id$="_0"]', thisQuestion).each(function(i) {
      handleSelects($(this));
    });
 
    // On submit
    $('#ls-button-submit').on('click', function(e) {
      $('select[id$="_1"]', thisQuestion).prop('disabled', false);
    });
  });
 
  $.fn.toggleOption = function(show) {
    jQuery(this).toggle(show);
    if(show) {
      if( jQuery(this).parent('span.toggleOption').length)
        jQuery(this).unwrap();
    } else {
      if(jQuery(this).parent('span.toggleOption' ).length == 0)
        jQuery(this).wrap( '<span class="toggleOption" style="display: none;" />');
    }
  };
</script>

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