Please help us help you and fill where relevant:
Your LimeSurvey version: 6.16.4
==================
I am trying to store the value of a JavaScript as an answer to a question.
Task: I want to randomize the order of a list (radio) question AND save the order in which it was randomized.
Idea: Use JavaScript to visually randomize the order of answers in question Q1, scan the DOM for the order and save the order (eg. A02, A03, A01) as an answer to the hidden question Q1order.
Script:
Code:
(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'));
})();
When I check the console, I can see that the values for hiddenField are correct.
I set up a question Q1order (equation question), left the question text empty and used Q1order as the equation.
I created a question Q1Debug (equation question), question text: "Show Order: {Q1Debug}" and used Q1order as the equation.
My Problem: The value that should be in Q1order (the order of the question) is not shown in Q1Debug. Do I have to use a variant of Q1order.show ?
I tried: Using Q1 and Q1order/Q1Debug on different pages (I am using show questions by group), tried to use it in the preview mode and in an activated survey. I am pretty sure the order is calculated in the right way, I just dont know how to to get it "out of the script" into the visible part of Limesurvey.
I am quite new to using scripts in limesurvey and I am a bit confused between the difference of curly brackets in the question text, equation text and general use of the expression script.