- Posts: 6
- Thank you received: 0
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
- Topic Author
- Offline
- New Member
Less
More
3 weeks 1 day ago - 3 weeks 1 day ago #262612
by ybaver
How to Enforce Randomized Order in a Multi-Choice Question? was created 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:
q21 question js code:
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: 3 weeks 1 day ago by ybaver.
Please Log in to join the conversation.
- Joffm
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 13068
- Thank you received: 4023
3 weeks 1 day ago #262735
by Joffm
Volunteers are not paid.
Not because they are worthless, but because they are priceless
Replied by Joffm on topic How to Enforce Randomized Order in a Multi-Choice Question?
Hi,
here a lot of examples
[url] forums.limesurvey.org/forum/can-i-do-thi...-following-questions [/url]
Joffm
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.