Welcome to the LimeSurvey Community Forum

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

Lock answer option to start of the list

  • tixeon
  • tixeon's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
4 years 9 months ago #186395 by tixeon
[Version 3.17.7 hosted]

I regularly use the following script to anchor/lock the position of an answer code (e.g. Don't know) to the bottom of a randomised list after other-specify, and it works well. However, this time I need to anchor the FIRST answer option, have other-specify as the last option, and randomise all of the other answer options.

I have tinkered with this script as best I can but don't seem to be able to get it to work. Any ideas?
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).append(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>
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 9 months ago #186548 by Joffm
Replied by Joffm on topic Lock answer option to start of the list
Easiest:
Replace "append" by "prepend" in section
// Place the last n answers created at the end of the list

Now the last n answers created are placed at the beginning of the list.

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose