Welcome to the LimeSurvey Community Forum

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

Fixed the answeat the end and the top

  • RitaShen
  • RitaShen's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
4 months 1 week ago #253617 by RitaShen
Fixed the answeat the end and the top was created by RitaShen
Please help us help you and fill where relevant:
Your LimeSurvey version: [see right hand bottom of your LimeSurvey admin screen]
Own server or LimeSurvey hosting: 5.6.49
Survey theme/template:LimeSurvey Bootwatch Theme
==================
Hi there,

I know that there is already a script to fix the last answer option when randomizing it.
Does anyone of you know could we / how to fix the answer at the end and the top at the same time?

for example,
1(fixed)
3
2
4
5(fixed)

1(fixed)
2
4
3​​​
5(fixed)

Thank you very much!

Please Log in to join the conversation.

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 months 1 week ago #253624 by tpartner
Replied by tpartner on topic Fixed the answeat the end and the top
What question type? Can you attach a small sample survey (.lss file) containing only the relevant questions?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

  • RitaShen
  • RitaShen's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
4 months 1 week ago #253631 by RitaShen
Replied by RitaShen on topic Fixed the answeat the end and the top
 Dear tpartner,

many thanks, I just attached a sample survey,
In the question, E & F have already been fixed at the end.
Now, I want A to be fixed at the top of options

File Attachment:

File Name: limesurvey...1546.lss
File Size:25 KB


 

Please Log in to join the conversation.

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 months 1 week ago - 4 months 5 days ago #253642 by tpartner
Replied by tpartner on topic Fixed the answeat the end and the top
This script, in the question source of a list-radio question, will fix the answer codes defined in the fixedStartCodes variable to the start of the answers list and the codes in the fixedEndCodes variable to the end of the list.

Code:
<script type="text/javascript" data-author="Tony Partner">  
 
  $(document).on('ready pjax:scriptcomplete',function(){
 
    // The answer codes of item(s) to be fixed
    var fixedStartCodes = [1];
    var fixedEndCodes = [4,5,6];
 
    // Set this to "true" if you want "Other" to be fixed in the last position
    var otherFixed = false;
 
    // Identify this question
    var qID = '{QID}';
    var thisQuestion = $('#question'+qID);
 
    // Fix the item(s) to the start
    $.each(fixedStartCodes, function(i, code) {
      $('.answer-item[id$="X'+qID+code+'"]').prependTo($('.answers-list ul:eq(0)', thisQuestion));
    });    
 
    // Fix the item(s) to the end
    $.each(fixedEndCodes, function(i, code) {
      $('.answer-item[id$="X'+qID+code+'"]').appendTo($('.answers-list ul:eq(0)', thisQuestion));
    });    
 
    // 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>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 4 months 5 days ago by tpartner. Reason: Update code

Please Log in to join the conversation.

  • RitaShen
  • RitaShen's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
4 months 1 week ago #253672 by RitaShen
Replied by RitaShen on topic Fixed the answeat the end and the top
Dear Tony,

Thank you very much for your assistance. I tried the syntax you provided, but I couldn't present it smoothly.

In theory, it should be presented this way,
 
but in practice, when I conduct tests, it appears like this.
 
 

Please Log in to join the conversation.

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 months 1 week ago #253720 by tpartner
Replied by tpartner on topic Fixed the answeat the end and the top
Did you try the attached sample survey?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

  • RitaShen
  • RitaShen's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
4 months 1 week ago #253722 by RitaShen
Replied by RitaShen on topic Fixed the answeat the end and the top
I sincerely apologize. I do recall that it was supposed to be included, but I might have inserted it before sending it out.
The following is an attachment 

File Attachment:

File Name: limesurvey...2-20.lss
File Size:70 KB

Please Log in to join the conversation.

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 months 1 week ago #253723 by tpartner
Replied by tpartner on topic Fixed the answeat the end and the top
Did you try my sample survey.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

  • RitaShen
  • RitaShen's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
4 months 1 week ago #253724 by RitaShen
Replied by RitaShen on topic Fixed the answeat the end and the top
yes I've tried your sample survey, and it can work,
I directly copied the syntax directly from the sample survey and adjusted // The answer codes of item(s) to be fixed
var fixedStartCodes = [1];
var fixedEndCodes = [4, 5, 6];
Due to this question having conditional settings, I am unsure if this will have any implications.

Please Log in to join the conversation.

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 months 5 days ago #253816 by tpartner
Replied by tpartner on topic Fixed the answeat the end and the top
This will handle multiple instances on a page:

Code:
<script type="text/javascript" data-author="Tony Partner">  
 
  $(document).on('ready pjax:scriptcomplete',function(){
 
    // The answer codes of item(s) to be fixed
    var fixedStartCodes = [1];
    var fixedEndCodes = [4,5,6];
 
    // Set this to "true" if you want "Other" to be fixed in the last position
    var otherFixed = false;
 
    // Identify this question
    var qID = '{QID}';
    var thisQuestion = $('#question'+qID);
 
    // Fix the item(s) to the start
    $.each(fixedStartCodes, function(i, code) {
      $('.answer-item[id$="X'+qID+code+'"]').prependTo($('.answers-list ul:eq(0)', thisQuestion));
    });    
 
    // Fix the item(s) to the end
    $.each(fixedEndCodes, function(i, code) {
      $('.answer-item[id$="X'+qID+code+'"]').appendTo($('.answers-list ul:eq(0)', thisQuestion));
    });    
 
    // 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>

Sample survey attached: 

File Attachment:

File Name: limesurvey...5271.lss
File Size:70 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

Lime-years ahead

Online-surveys for every purse and purpose