Welcome to the LimeSurvey Community Forum

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

Anchor subquestions on top of random order array

  • Matadeleo
  • Matadeleo's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
7 years 3 months ago #158237 by Matadeleo
Hi guys,

I'm trying to anchor a specific sub-question on top of an array that has been randomised.

I've attached an example survey that has an example array and the option that should be "fixed" to the top has a note.

File Attachment:

File Name: anchoring.lss
File Size:17 KB


If it helps, here is some similar code used to fix answer options to bottom of a list:
Code:
<script type="text/javascript" charset="utf-8">  
 
  $(document).ready(function() {
 
    // The number of answers to be fixed at the end of the list
    var fixedAnswers = 2;
 
    // Identify this question
    var qID = {QID}; 
 
    // Find the number of answers
    var ansCount = $('#question'+qID+' li.answer-item').length;
 
    // 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$="X'+qID+(ansCount-fixedIndex)+'"]');
      var answerItem = $(answer).closest('li');
      var answersList = $(answer).closest('ul');
      $(answersList).append(answerItem);
      fixedIndex--;
    }    
  });
</script>
Attachments:
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 3 months ago - 7 years 3 months ago #158246 by tpartner
This should do the trick:

Code:
<script type="text/javascript" charset="utf-8">
  $(document).on('ready pjax:complete',function() {
 
    // Define the subquestion code to be fixed at the top of the array
    var fixedCode = '1';
 
    // Move that row to the top
    $('#question{QID} table.subquestion-list tbody:eq(0)').prepend($('tr[id$="X{QID}'+fixedCode+'"]'));
 
    // Fix up the row background colours
    $('#question{QID} tr').each(function(i){
      $(this).removeClass('array1, array2');
      if(i % 2 == 0) {
        $(this).addClass('array1');
      }
      else {
        $(this).addClass('array2');
      }
    });
  });
</script>

Here's your survey back with that script included in the question text.

File Attachment:

File Name: limesurvey...7193.lss
File Size:18 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 7 years 3 months ago by tpartner.
The following user(s) said Thank You: Matadeleo
The topic has been locked.
  • Matadeleo
  • Matadeleo's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
7 years 2 months ago #158335 by Matadeleo
Replied by Matadeleo on topic Anchor subquestions on top of random order array
Works perfectly, thanks tpartner
The topic has been locked.
Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose