Please help us help you and fill where relevant:
Your LimeSurvey version: LimeSurvey Community Edition version 6.15.11
Own server or LimeSurvey hosting:
Survey theme/template:
==================Hello everyone,I'm facing an issue with a JavaScript snippet in LimeSurvey and would appreciate some guidance.
My Goal: I'm using a question with a randomized answer order, and I need to keep the last answer (or a specific number of answers) in a fixed position at the end of the list.
The Problem: I have a JavaScript code that accomplishes this perfectly. When I place it inside
tags in the question's source editor, it works as expected. However, after saving the question, the built-in XSS filter strips the
and
tags, causing the code to be rendered as plain text within the question body. For security reasons, I need to keep the global XSS filtering enabled.
What I've Tried:
- Using the dedicated 'Script' tab: I tried pasting the JavaScript code (without the
tags) into the question's 'Script' section in the advanced settings. Unfortunately, the script doesn't seem to execute from there and has no effect.
- Using a custom CSS class and
: My other idea was to add a custom CSS class (e.g.,
) to the question. Then, I attempted to modify my template's
file to detect questions with this class and apply the script to them. I wasn't able to get this approach to work, but perhaps I implemented it incorrectly.
My Question: What is the recommended way to implement question-specific JavaScript like this without it being stripped by the XSS filter? Is modifying the template's
based on a CSS class the right path, and if so, what would be the correct way to target the question and execute the script?
My JavaScript Code: This is the code I'm trying to use:
Code:
$(document).on('ready pjax:scriptcomplete',function(){
// The number of answers to be fixed at the end of the list
var fixedAnswers = 1;
// Set this to "true" if you want "Other" to be fixed in the last position
var otherFixed = false;
// Identify this question
var qID = {QID};
// Find the number of answers
var ansCount = $('#question'+qID+' .answer-item').length;
if($('#question'+qID+' input[type="text"]').length > 0) {
ansCount = ansCount -1
}
console.log(ansCount);
// 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^="answer"][id$="X'+qID+(ansCount-fixedIndex)+'"]');
var answerItem = $(answer).closest('.answer-item');
var answersList = $(answer).closest('ul');
$(answersList).append(answerItem);
fixedIndex--;
}
// Handle "Other"
if(otherFixed == true && $('#question'+qID+' input[type="text"]').length > 0) {
var otherAnswer = $('#question'+qID+' input[type="text"]');
var otherAnswerItem = $(otherAnswer).closest('.answer-item');
var otherAnswersList = $(otherAnswer).closest('ul');
$(otherAnswersList).append(otherAnswerItem);
}
});
Thank you in advance for any help or suggestions!