Defining embedded variable based on answer to multiple choice question using javascript | XM Community
Skip to main content
Question

Defining embedded variable based on answer to multiple choice question using javascript

  • August 21, 2024
  • 2 replies
  • 61 views

Forum|alt.badge.img+1

I have a variable, wait_yes, that I have defined in survey flow. In the survey there is a question with 4 choices and I want value for wait_yes to be set based on their choice. Pretty basic but I have little programming exp.

Let’s say answer choices are

  1. monday
  2. tuesday
  3. wednesday
  4. thursday

So if they select 3, then wait_yes = wednesday. What’s the javascript to accomplish this?

2 replies

chackbusch
QPN Level 5 ●●●●●
Forum|alt.badge.img+22
  • QPN Level 5 ●●●●●
  • 415 replies
  • August 22, 2024

@huu5hb Is there a reason you want to work with JavaScript? You can just add some embedded data block after the question block and assign the value to wait_yes with piped text. Click „Select a value now“ on the right side of the assignment and select the relevant question and „Selected Choices“ (screenshot is from the editor but the same menu is available in the survey flow):

If you really need to work with JavaScript, there are several threads, e.g., this one. In the JavaScript of the question in the onPageSubmit method you could place:

let sel = this.getSelectedChoices();

Qualtrics.SurveyEngine.setEmbeddedData(“wait_yes", sel);

Best

Christian


Forum|alt.badge.img+1
  • Author
  • 2 replies
  • August 22, 2024

Thanks Christian. I’m mostly interested in javascript because I have some variables that can be set by several questions in different branches of the survey. As the flow grows in complexity, it becomes harder to manager variables only there.