How to use JavaScript to amend the text of answer choices in a MC question? | XM Community
Question

How to use JavaScript to amend the text of answer choices in a MC question?

  • 2 April 2024
  • 0 replies
  • 19 views

Badge

So, just like the title. How do I use JavaScript to amend the text of answer choices in a MC question? 
It is how I setup my question:

It is my code:
 

Qualtrics.SurveyEngine.addOnload(function()
{
// Determine the current month and year
var now = new Date();
var currentMonth = now.getMonth(); // Note: January is 0, February is 1, and so on.
var currentYear = now.getFullYear();

// Define semester names and month ranges
var semesters = ['Winter', 'Spring', 'Summer', 'Fall'];
var nextSemesterIndex = 0; // Default to Winter

// Determine the next semester based on the current month
if (currentMonth >= 0 && currentMonth <= 3) { // Jan-Apr
nextSemesterIndex = 1; // Spring
} else if (currentMonth >= 4 && currentMonth <= 5) { // May-Jun
nextSemesterIndex = 2; // Summer
} else if (currentMonth >= 6 && currentMonth <= 8) { // Jul-Sep
nextSemesterIndex = 3; // Fall
}

var offset = 0;
if (nextSemesterIndex == 0) {
offset = 1;
}

// Iterate through the next four semesters, including the current one
for (var i = 0; i < 4; i++) {
var semesterIndex = (nextSemesterIndex + i) % semesters.length;
var yearOffset = Math.floor((nextSemesterIndex + i) / semesters.length);
var semesterText = semesters[semesterIndex] + ' ' + (currentYear + yearOffset + offset);

// Use jQuery to set the choice text
// Adjust the 'QID1216589079~2', 'QID1216589079~3', etc., to target the correct choices
jQuery("label[for='QR~QID1216589079~" + (i + 2) + "']").text(semesterText);

});

Here is a brief explanation of my code: It changes the four answer choices to the next semesters. Let say, it is now Winter 2024. Term 1 would be Spring 2024. Term 2 would be Summer 2024. Term 3 would be Fall 2024, and Term 4 would be Winter 2025.

It is how the answer choices looks like after I applied the code above:

Technically, it changed the labels successfully. However, the answers are no longer clickable. I have done lots of research, but I am still lost. I couldn’t even tell if it is possible to change the labels while keeping it clickable. Please, could anyone give me some advise? 


0 replies

Be the first to reply!

Leave a Reply