Welcome to the LimeSurvey Community Forum

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

Forced or Conditional Answers in Array

  • BBCMResearch
  • BBCMResearch's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
7 years 2 days ago #152591 by BBCMResearch
Forced or Conditional Answers in Array was created by BBCMResearch
Hi everyone,

In my next survey, I have an array with 8 sub-questions, and a 5 point agreement scale from "Strongly Disagree" to "Strongly Agree"

However, the array contains the following two questions:

SQ6: I identify with A more than B
SQ7: I identify with B more than A

Obviously, I would prefer not allowing users to say that they agree to both of these or disagree to both, and I am not allowed to edit the subquestion text or separated them from the array, which would have allowed for easier solutions.

Is it possible to grey out answers to SQ7 depending on how SQ6 is answered? For instance, if someone answers "Agree" on SQ6, then they are only allowed to answer strongly disagree, disagree, or neutral on SQ7?

Tony has offered scripts in the past here that allowed me to limit answers in general, so that some subquestions had 6 answers while others had 5, but those weren't conditional options.


Alternatively, my client proposed offering a 6th answer to SQ6 & 7 only - "I identify equally with both" that, if selected in SQ6, forces it to be selected in SQ7.


Any and all suggestions and solutions would be appreciated at this point. Thanks again for all you do.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 1 day ago #152613 by tpartner
Replied by tpartner on topic Forced or Conditional Answers in Array
LimeSurvey version?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • BBCMResearch
  • BBCMResearch's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
7 years 1 day ago #152623 by BBCMResearch
Replied by BBCMResearch on topic Forced or Conditional Answers in Array
Currently running Version 2.62.2+170303
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 1 day ago #152625 by tpartner
Replied by tpartner on topic Forced or Conditional Answers in Array
Placing the following script in the source of an array question will render the defined rows mutually exclusive:

Code:
<script type="text/javascript" charset="utf-8">
  $(document).ready(function() {     
    // Call the exclusiveArrayRows function with question and sub-question IDs
    var sqIDs = ['SQ6', 'SQ7']
    exclusiveArrayRows({QID}, sqIDs);
  });
 
  // A function to render array rows mutually exclusive
  // Parameters:
  ////  1) The question ID
  ////  2) An array of sub-question IDs to be exclusive
  function exclusiveArrayRows(qID, sqIDs) {
    // Identify the question
    var thisQuestion = $('#question'+qID);
 
    // Loop through the sub-question IDs
    $(sqIDs).each(function(i, val) {
      // Assign a class
      $('tr.answers-list[id$="X'+qID+val+'"]', thisQuestion).addClass('exclusive-row');
    });
 
    // Initial radio states
    $('.exclusive-row input.radio:checked', thisQuestion).each(function(i) {
      // Identify rows and values
      var thisValue = $(this).val();
      var thisRow = $(this).closest('tr.answers-list');
      var otherRows = $('tr.answers-list.exclusive-row', thisQuestion).not(thisRow);
 
      // Disable the appropriate radio(s) in the other rows(s)
      $('input.radio[value="'+thisValue+'"]', otherRows).prop('disabled', true);
    });
 
    // Listener on the exclusive radios
    $('.exclusive-row input.radio', thisQuestion).on('click', function(e) {
      // Identify rows and values
      var thisValue = $(this).val();
      var thisRow = $(this).closest('tr.answers-list');
      var otherRows = $('tr.answers-list.exclusive-row', thisQuestion).not(thisRow);
 
      // Reset the other rows(s)
      $('input.radio', otherRows).prop('disabled', false);
 
      // Disable the appropriate radio(s) in the other rows(s)
      $('input.radio[value="'+thisValue+'"]', otherRows).prop('disabled', true);
    });
  }
</script>

Sample survey attached:

File Attachment:

File Name: limesurvey...7377.lss
File Size:22 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.
  • BBCMResearch
  • BBCMResearch's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
7 years 1 day ago #152626 by BBCMResearch
Replied by BBCMResearch on topic Forced or Conditional Answers in Array
Tony,

Thank you for the excellent-as-always code. Question though - would it be possible to make the radios more than mutually exclusive? For example, if someone selects either 1 OR 2, I need both 1 AND 2 excluded in the other question. And if they select 4 OR 5, I likewise need both 4 AND 5 excluded.

Thanks again!
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 1 day ago - 7 years 1 day ago #152628 by tpartner
Replied by tpartner on topic Forced or Conditional Answers in Array
Sure, in that case, something like this:

Code:
<script type="text/javascript" charset="utf-8">
  $(document).ready(function() {     
    // Call the exclusiveArrayRows function with question and sub-question IDs
    var sqIDs = ['SQ6', 'SQ7']
    exclusiveArrayRows2({QID}, sqIDs);
  });
 
 
  // A function to conditionally disable array answers
  // Parameters:
  //    1) The question ID
  //    2) An array of sub-question IDs to be exclusive
  function exclusiveArrayRows2(qID, sqIDs) {
    // Identify the question
    var thisQuestion = $('#question'+qID);
 
    // Loop through the sub-question IDs
    $(sqIDs).each(function(i, val) {
      // Assign a class
      $('tr.answers-list[id$="X'+qID+val+'"]', thisQuestion).addClass('conditional-row');
    });
 
    // Initial radio states
    $('.conditional-row input.radio:checked', thisQuestion).each(function(i) {
      // Call a function to handle the radios
      handleRadios($(this), $(this).val());
    });
 
    // Listener on the exclusive radios
    $('.conditional-row input.radio', thisQuestion).on('click', function(e) {
      // Call a function to handle the radios
      handleRadios($(this), $(this).val(), true);
    });
 
    // A function to disable/enable radios
    function handleRadios(checkedRadio, thisValue, onClick) {
      var thisRow = $(checkedRadio).closest('tr.answers-list');
      var otherRows = $('tr.answers-list.conditional-row', thisQuestion).not(thisRow);
 
      if(onClick !== undefined) {
        // Reset the other rows on click
        $('input.radio', otherRows).prop('disabled', false);
      }
 
      if(thisValue != 3) {
 
        //Determine which radios to disable
        if(thisValue < 3) {
          var disabledInputs = $('input.radio', otherRows).filter(function(e) {
            return $(this).val() < 3;
          });
        }
        else {
          var disabledInputs = $('input.radio', otherRows).filter(function(e) {
            return $(this).val() > 3;
          });
        }
 
        // Disable the appropriate radios in the other rowss  
        $(disabledInputs).prop('checked', false).prop('disabled', true);
      }
    }
  }
</script>

Sample survey:

File Attachment:

File Name: limesurvey...3771.lss
File Size:22 KB



Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 7 years 1 day ago by tpartner.
The following user(s) said Thank You: BBCMResearch
The topic has been locked.
  • BBCMResearch
  • BBCMResearch's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
7 years 1 day ago #152629 by BBCMResearch
Replied by BBCMResearch on topic Forced or Conditional Answers in Array
Exactly what I need. One last request though. If I add a 6th answer "I identify with both equally" that only displays for SQ6 & SQ7, say using some of the code you provided to a previous post - how would I make that answer mutually inclusive - so that if it's selected on one, it's forced on the other?

Thanks again!
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 1 day ago #152630 by tpartner
Replied by tpartner on topic Forced or Conditional Answers in Array
Try this:

Code:
<script type="text/javascript" charset="utf-8">
  $(document).ready(function() {     
    // Call the exclusiveArrayRows function with question and sub-question IDs
    var sqIDs = ['SQ6', 'SQ7']
    exclusiveArrayRows2({QID}, sqIDs);
  });
 
 
  // A function to conditionally disable array answers
  // Parameters:
  //    1) The question ID
  //    2) An array of sub-question IDs to be exclusive
  function exclusiveArrayRows2(qID, sqIDs) {
    // Identify the question
    var thisQuestion = $('#question'+qID);
 
    // Loop through the sub-question IDs
    $(sqIDs).each(function(i, val) {
      // Assign a class
      $('tr.answers-list[id$="X'+qID+val+'"]', thisQuestion).addClass('conditional-row');
    });
 
    // Initial radio states
    $('.conditional-row input.radio:checked', thisQuestion).each(function(i) {
      // Call a function to handle the radios
      handleRadios($(this), $(this).val());
    });
 
    // Listener on the exclusive radios
    $('.conditional-row input.radio', thisQuestion).on('click', function(e) {
      // Call a function to handle the radios
      handleRadios($(this), $(this).val(), true);
    });
 
    // A function to disable/enable radios
    function handleRadios(checkedRadio, thisValue, onClick) {
      var thisRow = $(checkedRadio).closest('tr.answers-list');
      var otherRows = $('tr.answers-list.conditional-row', thisQuestion).not(thisRow);
 
      if(onClick !== undefined) {
        // Reset the other rows on click
        $('input.radio', otherRows).prop('disabled', false);
        $('input.radio[value="6"]', otherRows).prop('checked', false);
      }
 
      if(thisValue != 3) {
 
        //Determine which radios to disable
 
        if(thisValue == 6) {
          $('input.radio[value="'+thisValue+'"]', otherRows).prop('checked', true);
          $('input.radio', thisRow).prop('disabled', false);
        }
        else if(thisValue < 3) {
          var disabledInputs = $('input.radio', otherRows).filter(function(e) {
            return $(this).val() < 3;
          });
          // Disable the appropriate radios in the other rows  
          $(disabledInputs).prop('checked', false).prop('disabled', true);
        }
        else {
          var disabledInputs = $('input.radio', otherRows).filter(function(e) {
            return $(this).val() > 3 &amp;&amp; $(this).val() != 6;
          });
          // Disable the appropriate radios in the other rows  
          $(disabledInputs).prop('checked', false).prop('disabled', true);
        }        
      }
      else {
        //$('input.radio', thisRow).prop('disabled', false);
      }
    }
 
    // Listener on the radio cells
    $('.conditional-row .answer-item', thisQuestion).on('click', function(e) {
      if($('input.radio', this).is(':disabled')) {
        return false;
      }
    });
  }
</script>

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: BBCMResearch
The topic has been locked.
  • BBCMResearch
  • BBCMResearch's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
7 years 1 day ago #152631 by BBCMResearch
Replied by BBCMResearch on topic Forced or Conditional Answers in Array
Quality work as always. Cheers!
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose