Help with Randomizing Question Text and Answer Order Using JavaScript | XM Community
Skip to main content

Hi all,

I'm trying to randomly change the text and the order of answer options in a single multiple-choice question using JavaScript.

Here's what I'm trying to do:

  1. Randomly change the question text:

    • Option 1: "Here is a 7-point scale on which the political views that people might hold are arranged from extremely liberal to extremely conservative."
    • Option 2: "Here is a 7-point scale on which the political views that people might hold are arranged from extremely conservative to extremely liberal."
  2. Reorder the multiple-choice options based on the question text:

    • If the question starts with "extremely liberal", the options should be in this order:
      • Extremely Liberal
      • Liberal
      • Slightly Liberal
      • Moderate, Middle of the Road
      • Slightly Conservative
      • Conservative
      • Extremely Conservative
      • Don’t Know, Haven’t Thought (always last)
    • If the question starts with "extremely conservative", the options should be:
      • Extremely Conservative
      • Conservative
      • Slightly Conservative
      • Moderate, Middle of the Road
      • Slightly Liberal
      • Liberal
      • Extremely Liberal
      • Don’t Know, Haven’t Thought (always last)

The problem:

  • I can randomize the question text, but the options don’t reorder dynamically based on the text.
  • I’m using this.setChoiceLabel(i, label) in JavaScript, but it seems like the changes aren’t being applied.

What I need help with:

  1. Can JavaScript dynamically reorder answer options in Qualtrics?
  2. Is there something that prevents the changes from taking effect after the question is loaded?
  3. What’s the best way to achieve this in a single question?

Thanks in advance for your help!

Hi @M414  you can achieve this use case without Javascript as well with the help of randomizer, embedded data and pipe text.

STEP 1: Create a set of embedded data for your question text and answer choices.

STEP 2: Insert this set into a group element.

STEP 3: Duplicate step 1 and step 2 but change the question text and the answer choices.

STEP 4: Place both these groups under a randomizer element.

Tick off the ‘evenly present elements’ so that the random selection is fair. Keep the value as 1, so that any one of the group will be selected randomly.

STEP 5: Create a multi choice question using pipe text values.

This process will give you the exact requirement you wanted.

 

Please Note, how the collected response data will look like.

 

 

You can also create an extra embedded data in the survey flow just to identify which group was selected when and how many times. Later on , you can use it as a page filter on your dashboard as well.


END!


Create two separate questions. 1 for extremely liberal add the respective answer options and do the same for Conservative. 

Then, in the survey flow you can use the randomizer to display the questions.

 

You don’t need JS for this.


Thank you for your helpful replies, @omkarkewat and @jbk!


@M414,

From a data perspective, you would be better off using JS rather than piped data or multiple questions.

JS can reorder the question’s choices. I don’t think setChoiceLabel() is a Qualtrics function (it isn’t in the documentation).  The specific JS would depend on the layout you are using (Simple or something else).

 


Thanks @TomG!


Leave a Reply