Welcome to the LimeSurvey Community Forum

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

Alternate ranking question - Dynamic answer options

More
7 years 10 months ago #159920 by tpartner
Oh, I see your problem now - the filtering is working but the piping is not. Something in the style declaration is messing with Expression Manager.

Use this script. Note that in the style rules you must leave a space after the opening curly braces and before the closing braces.

Code:
<script type="text/javascript" charset="utf-8"> 
  $(document).on('ready pjax:complete',function() {
 
    // Identify this question
    var thisQuestion = $('#question{QID}');
 
    // Define some "Choose" text
    var choosetext = 'Please choose...';
 
    // Number of visible rows
    var rowCount = $('tr.answer-item:visible', thisQuestion).length;
 
    // Loop through all drop-downs
    $('tr.answer-item select', thisQuestion).each(function(i) {
 
      // Reset if value is above the number of visible rows
      if($(this).val() > rowCount) {
        $(this).prepend('<option value="">'+choosetext+'</option>').val('');
        checkconditions(this.value, this.name, this.type);
      }
 
      // Remove all unnecessary options
      $('option', this).filter(function() {
        return this.value > rowCount;
      }).remove(); 
    });
 
    // Listener on the dropdowns
    $('tr.answer-item select', thisQuestion).on('change', function(e) {
      // Handle non-unique answers
       handleDuplicates();
    });
 
    function handleDuplicates() {
      $('.question-item', thisQuestion).removeClass('duplicate-row');
      $('tr.answer-item select', thisQuestion).each(function(i) {
        if(!$(this).closest('.question-item').hasClass('duplicate-row') &amp;&amp; $(this).val() != '') {
          var thisSelect = $(this);
          var selectedValue = $(this).val();
          $('tr.answer-item select', thisQuestion).not(this).each(function(i) {
            if($(this).val() == selectedValue) {
              $(this).closest('.question-item').addClass('duplicate-row');
              $(thisSelect).closest('.question-item').addClass('duplicate-row');
            }
          });
        }
      });
    }
    handleDuplicates();
 
    //Insert new style rules
    var newStyle = '<style type="text/css">\
              .duplicate-row { background-color: #E74C3C; }\
              .duplicate-row .answertext { color: #FFFFFF; }\
            </style>';    
    $("head link[rel='stylesheet']").last().after(newStyle);
  });
</script>

Working survey attached:

File Attachment:

File Name: limesurvey...0-21.lss
File Size:47.28 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.
More
7 years 10 months ago #159946 by Gensz78
Thank you so much! It's working beautifully now!

Gen
The topic has been locked.
Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose