Welcome to the LimeSurvey Community Forum

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

How to Enforce Randomized Order in a Multi-Choice Question?

  • ybaver
  • ybaver's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
2 weeks 3 days ago - 2 weeks 3 days ago #262612 by ybaver
LimeSurvey version:3 .23
Hello everyone!

I am working with a multi-choice question (q21) that includes 15 elements. I want these elements to appear in a random order that I can track. To achieve this, I first created a short free-text question (named orderq21) to generate a random order between 1 and 15. This part of the process is functioning perfectly.

However, I'm encountering a problem when trying to apply this random order to q21. Despite my efforts, q21 does not reflect the randomized sequence generated by orderq21.

Could anyone assist me with this issue? I am attaching my .lss file and the JavaScript code I used for these two questions.

Thank you in advance for your help!

orderq21 question js code:
Code:
<script>
function shuffleArray(array) {
    for (let i = array.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [array[i], array[j]] = [array[j], array[i]]; 
    }
}
function fillInputWithRandomNumbers() {
    var numbers = ;
    for (let i = 1; i <= 15; i++) {
        numbers.push(i);
    }
    shuffleArray(numbers);
    var randomNumbersString = numbers.join(", ");
   document.getElementById("answer" + "{SGQ}").value = randomNumbersString;
}
 
document.addEventListener('DOMContentLoaded', fillInputWithRandomNumbers);
</script>[/i]

q21 question js code:

Code:
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
    var questionID = '{QID}'; 
    var orderData = '{orderq21}'; 
    var randomOrder = orderData.split(', ').map(Number); 
 
    function reorderItems() {
        var parentUl = $('#question' + questionID + ' ul'); 
        var itemsMap = {}; 
 
    
        parentUl.children('li').each(function() {
            var idNum = parseInt(this.id.replace('javatbd' + questionID, ''), 10);
            itemsMap[idNum] = $(this); 
        });
 
    
        parentUl.empty();
 
    
        randomOrder.forEach(function(id) {
            if (itemsMap[id]) { 
                parentUl.append(itemsMap[id]); 
            }
        });
    }    
    reorderItems();
});
</script>

Attachment not found

Last edit: 2 weeks 3 days ago by ybaver.

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 weeks 3 days ago #262735 by Joffm
Hi,
here a lot of examples
[url] forums.limesurvey.org/forum/can-i-do-thi...-following-questions [/url]

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

Lime-years ahead

Online-surveys for every purse and purpose