Welcome to the LimeSurvey Community Forum

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

Fix first answer option with randomizing

  • jrddhl
  • jrddhl's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 4 months ago #208530 by jrddhl
Dear community,

I know that there is already a script to fix the last answer option when you randomize it. Does anyone of you know if there is another scritp / workaround to fix the first answer option and all other options are still randomizing?

Thank you very much!
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 4 months ago #208531 by Joffm
Replied by Joffm on topic Fix first answer option with randomizing
Hi,
no other script.
Just some kind of adaption.
If the script uses the function "append" to fix an item at the bottom, so why not try to use "prepend" to fix it at the top.

So replace the "append" in this line
$(answersList).append(answerItem);
to
$(answersList).prepend(answerItem);




Code:
<script type="text/javascript" charset="utf-8">  
 
  $(document).on('ready pjax:scriptcomplete',function(){
 
    // The number of answers to be fixed at the end of the list
    var fixedAnswers = 1;
 
    // Set this to "true" if you want "Other" to be fixed in the last position
    var otherFixed = false;
 
    // Identify this question
    var qID = {QID}; 
 
    // Find the number of answers
    var ansCount = $('#question'+qID+' .answer-item').length;
    if($('#question'+qID+' input[type="text"]').length > 0) {
      ansCount = ansCount -1
    }
    console.log(ansCount);
 
    // Place the last n answers created at the end of the list
    var fixedIndex = fixedAnswers - 1;
    for (var i=0; i<fixedAnswers; i++) {
      var answer = $('input[id^="answer"][id$="X'+qID+(ansCount-fixedIndex)+'"]');
      var answerItem = $(answer).closest('.answer-item');
      var answersList = $(answer).closest('ul');
      $(answersList).prepend(answerItem);
      fixedIndex--;
    }    
 
    // Handle "Other"
    if(otherFixed == true &amp;&amp; $('#question'+qID+' input[type="text"]').length > 0) {
      var otherAnswer = $('#question'+qID+' input[type="text"]');
      var otherAnswerItem = $(otherAnswer ).closest('.answer-item');
      var otherAnswersList = $(otherAnswer ).closest('ul');
      $(otherAnswersList).append(otherAnswerItem);
    }
  });
</script>

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The following user(s) said Thank You: tpartner
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose