Prepend Text to Multiple Choice Question Options | XM Community
Solved

Prepend Text to Multiple Choice Question Options


Userlevel 2
Badge

I have many multiple-choice questions with four options. I need to update them all to have the prefixes A., B., C., D. before each option. I'm thinking the easiest way to do this en masse would be to paste javascript into each question instead of updating all of the choice texts.
How can I add A, B, C, and D to the options in my multiple choice questions using JS?

icon

Best answer by ahmedA 8 May 2021, 22:24

View original

4 replies

Userlevel 7
Badge +21

If you want to apply it to all questions, then add this to your header:
Qualtrics.SurveyEngine.addOnReady(function () {
    let choices = document.querySelectorAll("span [for].MultipleAnswer,span [for].SingleAnswer");
    let alphabet = new Array(26).fill(1).map((_, i) => String.fromCharCode(65 + i));


    choices.forEach((choice) => {
        let pos = parseInt(choice.id.split("-")[1]) - 1;
        choice.innerText = alphabet[pos] + ". " + choice.innerText;
    }); 
});

If its just for some questions, then you'll need to add it to each question individually, and replace

document
with
this.questionContainer

If you want to use lower case then change 65 to 97

Userlevel 2
Badge

ahmedA I have a few questions in my survey that have images as answer choices. In this example, the letters are replacing the images. Do you have any advice on how to handle these? I tried setting

innerText
to
innerHTML
with no luck.

Userlevel 7
Badge +21

innerHTML 
should work, you could also try
outerHTML
, but that may change the formatting. If it doesn't share the survey link without the code applied, maybe I'm missing something.

Userlevel 2
Badge

I was able to solve the image HTML problem by setting both parts to `innerHTML`
  choices.forEach((choice) => {
    let pos = parseInt(choice.id.split("-")[1]) - 1;
    choice.innerHTML = alphabet[pos] + ". " + choice.innerHTML;
  }); 

Leave a Reply