- Posts: 4
- Thank you received: 0
Ask the community, share ideas, and connect with other LimeSurvey users!
(function () { console.log('script started'); var qid = {QID}; console.log('qid:', qid); var question = document.getElementById('question' + qid); console.log('question:', question); if (!question) { console.log('Question container not found'); return; } if (question.dataset.orderRandomized === 'true') { console.log('Already randomized'); return; } question.dataset.orderRandomized = 'true'; function shuffle(array) { for (var i = array.length - 1; i > 0; i--) { var j = Math.floor(Math.random() * (i + 1)); var temp = array[i]; array[i] = array[j]; array[j] = temp; } return array; } var items = question.querySelectorAll('ul.answers-list > li.answer-item, table.question tbody tr.answer-item'); console.log('items found:', items.length); if (!items.length) { console.log('No answer items found'); return; } var normalItems = []; var fixedItems = []; for (var k = 0; k < items.length; k++) { var item = items[k]; var input = item.querySelector('input[type="radio"], input[type="checkbox"]'); var code = input ? input.value : null; console.log('code:', code); if (code === 'OTHER' || code === 'NONE' || code === 'AO06') { fixedItems.push(item); } else { normalItems.push(item); } } shuffle(normalItems); var finalItems = normalItems.concat(fixedItems); var containerUl = question.querySelector('ul.answers-list'); var containerTbody = question.querySelector('table.question tbody'); var container = containerUl || containerTbody; console.log('container:', container); if (!container) { console.log('No answer container found'); return; } for (var m = 0; m < finalItems.length; m++) { container.appendChild(finalItems[m]); } var orderCodes = []; for (var n = 0; n < finalItems.length; n++) { var input2 = finalItems[n].querySelector('input[type="radio"], input[type="checkbox"]'); if (input2 && input2.value) { orderCodes.push(input2.value); } } console.log('final order:', orderCodes); var all = document.querySelectorAll('input, textarea'); var hiddenField = document.querySelector('[id*="{Q1order.sgqa}"]') || document.querySelector('[name*="{Q1order.sgqa}"]'); if (!hiddenField) { console.log('Hidden field not found'); return; } console.log('hiddenField:', hiddenField); if (!hiddenField) { console.log('Hidden field not found'); return; } hiddenField.value = orderCodes.join('|'); hiddenField.dispatchEvent(new Event('input', { bubbles: true })); hiddenField.dispatchEvent(new Event('change', { bubbles: true })); console.log('Saved order:', hiddenField.value); console.log(document.querySelectorAll('input')); })();
Please Log in to join the conversation.
Please Log in to join the conversation.
Please Log in to join the conversation.
Store the random order in a (hidden) question of type "short text" with this scriptIf you search the forums, you should find many examples of storing the randomization order.
<script type="text/javascript" charset="utf-8"> $(document).on('ready pjax:scriptcomplete',function(){ //Identify the questions var thisQuestion = $('#question{QID}'); var hiddenQuestion = $(thisQuestion).nextAll('.text-short:eq(0)'); // Create an array of answer codes var answerCodes = []; $('li.answer-item', thisQuestion).each(function(i) { answerCodes.push($(this).attr('id').split('X{QID}')[1]); }); // Load the hidden question $('input:text', hiddenQuestion).val(answerCodes); }); </script>
Please Log in to join the conversation.
Thank you guys very much! I am currently using the script Joffm provided and it is working so far. I will use it to dive deeper into what I want to do! Thank you so much!If you search the forums, you should find many examples of storing the randomization order.
I have not fully debugged your script, but you cannot programmatically load equation type questions. You will need to load your values into a hidden (via JS or CSS) text question. If you need ExpressionScript to recognize those loaded/updated values before submitting the page, fire a .change() event on the text input.
Please Log in to join the conversation.
None, except in the script field you don't wrap it in a <script> tag.Whats the difference between using the script in the question field and/or using the script field?
Please Log in to join the conversation.