Welcome to the LimeSurvey Community Forum

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

Randomize answers except a few at bottom

More
7 years 2 months ago #157565 by ARei
Hello!

Unfortunately not having any knowledge in JavaScript, I'd appreciate your assistance with the following:

I want to randomize a set of answers, but keeping a specific amount fixed at the bottom. Most often there are two answer options ("don't know" and "no answer", whereas the "no answer"-option isn't the default option but programmed manually). Sometimes there is an additional "none of the above". Ideally they should all show up under the "Other"-field.

I have found the following script from this thread:
Code:
script type=textjavascript charset=utf-8
  $(document).ready(function() {
 
     Identify this question
    var qID = {QID};
    var thisQuestion = $('#question'+qID);
 
     Define the sub-question code to be placed last
    var lastItem = 'SQ004';
 
     Move that item to the end of the list
    $('.question-item[id$=X'+qID+lastItem+']', thisQuestion).addClass('last-item').parent().appendTo($('.subquestion-list', thisQuestion));
  });
script

Interestingly, this was the only code that worked for us. It was also a rather simple code, which is important to us as it should be easy and quick to modify and ideally easy to understand for multiple people.

Of course, this solution is only for one answer option, not for 2 or 3 and, so I understand, for multiple choice questions.
We would need a code that works for both array and multiple choice options.


My question is: is it possible to modify this code so it works for 2 or optionally 3 options? And if so, how?


Thanks in advance for the answers!
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 2 months ago #157567 by tpartner
Replied by tpartner on topic Randomize answers except a few at bottom
Assuming you are using the latest LimeSurvey version, this will fix a number of sub-questions to the bottom for both multi-choice and array type questions.

Code:
<script type="text/javascript" charset="utf-8">
  $(document).ready(function() {
 
    // Identify this question
    var qID = {QID};
    var thisQuestion = $('#question'+qID);
 
    // Define the sub-question codes to be placed last
    var lastItems = ['SQ006', 'SQ007', 'SQ008'];
 
    // Loop through those sub-question codes 
    $.each(lastItems, function(i, val) {
      // Move that item to the end of the list
      // Multi-choice question
      if($(thisQuestion).hasClass('multiple-opt')) {
        $('.question-item[id$=X'+qID+val+']', thisQuestion).parent().appendTo($('.subquestion-list', thisQuestion));
      }
      // Array question
      if($(thisQuestion).hasClass('array-flexible-row')) {
        $('.answers-list[id$=X'+qID+val+']', thisQuestion).appendTo($('table.subquestion-list', thisQuestion));
      }
    });  
  });
</script>

Sample survey attached

File Attachment:

File Name: limesurvey...8-14.lss
File Size:25 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: ARei
The topic has been locked.
More
7 years 2 months ago #157575 by ARei
Replied by ARei on topic Randomize answers except a few at bottom
Thank you very much for the help! This works very well.

I tried to figure out on my own how to do the same for a List (radio) but couldn't figure out how to modify the last line of the code. And what the class is called exactly for the IF-command.

If you could help me once more I'd greatly appreciate it!
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 2 months ago #157589 by tpartner
Replied by tpartner on topic Randomize answers except a few at bottom
This should work for list-radio questions as well:

Code:
<script type="text/javascript" charset="utf-8">
  $(document).ready(function() {
 
    // Identify this question
    var qID = {QID};
    var thisQuestion = $('#question'+qID);
 
    // Define the sub-question codes to be placed last
    var lastItems = ['SQ006', 'SQ007', 'SQ008'];
 
    // Loop through those sub-question codes 
    $.each(lastItems, function(i, val) {
      // Move that item to the end of the list
      // Multi-choice question
      if($(thisQuestion).hasClass('multiple-opt')) {
        $('.question-item[id$=X'+qID+val+']', thisQuestion).parent().appendTo($('.subquestion-list', thisQuestion));
      }
      // Array question
      if($(thisQuestion).hasClass('array-flexible-row')) {
        $('.answers-list[id$=X'+qID+val+']', thisQuestion).appendTo($('table.subquestion-list', thisQuestion));
      }
      // List-radio question
      if($(thisQuestion).hasClass('list-radio')) {
        $('.answer-item[id$=X'+qID+val+']', thisQuestion).appendTo($('.answers-list', thisQuestion));
      }
    });  
  });
</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: ARei
The topic has been locked.
More
7 years 2 months ago #157703 by ARei
Replied by ARei on topic Randomize answers except a few at bottom
Thank you very much for the help! Works great.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
6 years 6 months ago #167439 by tpartner
Replied by tpartner on topic Randomize answers except a few at bottom
Here is an updated version of this script for LimeSurvey 3.x:

Code:
<script type="text/javascript" charset="utf-8">
  $(document).on('ready pjax:scriptcomplete',function(){
 
    // Identify this question
    var qID = {QID};
    var thisQuestion = $('#question'+qID);
 
    // Define the sub-question codes to be placed last
    var lastItems = ['SQ006', 'SQ007', 'SQ008'];
 
    // Loop through those sub-question codes 
    $.each(lastItems, function(i, val) {
      // Move that item to the end of the list
      // Multi-choice question
      if($(thisQuestion).hasClass('multiple-opt')) {
        $('.question-item[id$=X'+qID+val+']', thisQuestion).appendTo($('.question-item[id$=X'+qID+val+']', thisQuestion).parent());
      }
      // Array question
      if($(thisQuestion).hasClass('array-flexible-row')) {
        $('.answers-list[id$=X'+qID+val+']', thisQuestion).appendTo($('table.subquestion-list tbody:last', thisQuestion));
      }
      // List-radio question
      if($(thisQuestion).hasClass('list-radio')) {
        $('.answer-item[id$=X'+qID+val+']', thisQuestion).appendTo($('.answer-item[id$=X'+qID+val+']', thisQuestion).parent());
      }
    });  
  });
</script>

Sample survey:

File Attachment:

File Name: limesurvey...4-17.lss
File Size:31 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: DenisChenu, Wappenquaul
The topic has been locked.
More
5 years 4 months ago - 5 years 4 months ago #186125 by Wappenquaul
Replied by Wappenquaul on topic Randomize answers except a few at bottom
Hello!

I used these solutions as well but would need the code now for the answer type "multiple options with comments". I assumed that this is it:
Code:
// Multi-choice question with comments
      if($(thisQuestion).hasClass('multiple-opt-comments')) {
        $('.question-item[id$=X'+qID+val+']', thisQuestion).parent().appendTo($('.subquestion-list', thisQuestion));
      }

But it's not working.

(Still using the old version of the code, not 3.x)

Could you help us out again?
Last edit: 5 years 4 months ago by Wappenquaul.
The topic has been locked.
Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose