- Posts: 22
- Thank you received: 0
Ask the community, share ideas, and connect with other LimeSurvey users!
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { // Identify this question var qID = {QID}; // Find the number of answers var ansCount = $('#question'+qID+' li.answer-item').length; // Place the last answer created at the end of the list var answer = $( 'input[id$="X'+qID+ansCount+'"]'); var answerItem = $(answer).closest('li'); var answersList = $(answer).closest('ul'); $(answersList).append(answerItem); }); </script>
<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>
fixPostions({'respondseCode1':'position1','respondseCode2':'position2'})
That would require removing all of the specified answers from the list and then re-inserting them sequentially. Probably best done with a small jQuery pluging.It will helpful if you can adapt your script so that it accepts an object specifying the response code and its position...
// A jQuery plugin to fix list-radio items in specific places // Requires a "positions" object with: // keys - numeric position, "first" or "last" // values - answer codes (function( $ ){ $.fn.lsRadioPositions = function(options) { var opts = $.extend( { positions: { } }, options); return this.each(function() { var thisQuestion = $(this); var thisID = thisQuestion.attr('id').replace(/question/, ''); var positionsArr = []; // Loop through the positions object $.each( opts.positions, function(position, code) { // Remove the option from the list $('li.answer-item[id$="X'+thisID+code+'"]').appendTo(thisQuestion); // Create an array of keys if (opts.positions.hasOwnProperty(position)) { if(position == 'first') { position = '1first'; } positionsArr.push(position); } }); positionsArr.sort(); // Loop through the positions array $.each( positionsArr, function(i) { position = this; if(position == '1first') { position = 'first'; } var thisCode = opts.positions[position] // Re-insert the option if(position == 'last') { $('ul.answers-list li.answer-item:last', thisQuestion).after($('li.answer-item[id$="X'+thisID+thisCode+'"]')); } else if(position == 1 || position == 'first') { $('ul.answers-list li.answer-item:first', thisQuestion).before($('li.answer-item[id$="X'+thisID+thisCode+'"]')); } else { $('ul.answers-list li.answer-item:eq('+(position-2)+')', thisQuestion).after($('li.answer-item[id$="X'+thisID+thisCode+'"]')); } }); }); }; })( jQuery );
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { // Apply the radioPositions plugin to this question $('#question{QID}').lsRadioPositions( { positions: { 2: 'A2', last: 'A6', first: 'A1' } } ); }); </script>