use javascript and embedded data to randomize question formulation | XM Community
Skip to main content
Solved

use javascript and embedded data to randomize question formulation

  • February 13, 2020
  • 4 replies
  • 335 views

Hello, I am new to Qualtrics and I am struggling to understand how changing the embedded data works. I have the following situation. Let's say we have: Q1: What's your best friend's name? Q2: What's your brother's name? and in Q3 you want to ask how old is either the brother or the best friend (picking one of these randomly). What I am currently doing is first in the SurveyFlow I create an embedded data called "chooseRandom" (with no preset value), then, in Q3 I ask "How old is ${e://Field/chooseRandom}?" and I add the following javascript function: Qualtrics.SurveyEngine.addOnload(function() { var arr = ["${q://QID1/ChoiceTextEntryValue}", "${q://QID2/ChoiceTextEntryValue}"]; #create an array with the two names Qualtrics.SurveyEngine.setEmbeddedData('chooseRandom', arr[Math.floor(Math.random() * arr.length)]); #set 'chooseRandom' as one of those two elements }); However, when I preview the survey the question shows simply as "how old is ?" I think I'm simply not fully understanding how this "setEmbeddedData" function works. This is a simplified case, ideally I would want to have more conditional choices when creating the array from where to pick an element randomly, however I think if I understand this part, I can figure out the rest. Thanks in advance!

Best answer by ilir_k

From what I understand now, when I set the embedded data element via javascript, it only shows up starting from the next block. A bit annoying, but I think it should still work in my case.
View original

4 replies

  • Author
  • 4 replies
  • Answer
  • February 14, 2020
From what I understand now, when I set the embedded data element via javascript, it only shows up starting from the next block. A bit annoying, but I think it should still work in my case.

Akdashboard
Level 4 ●●●●
Forum|alt.badge.img+6
  • Level 4 ●●●●
  • 488 replies
  • February 14, 2020
I think you may be over engineering this. You don't need JavaScript in order to set randomization as it is a core function of the platform. You can either do it in the Survey Flow and set an embedded data value to something, or use the randomization option at the block-level to randomly display Q1 or Q2. This explains the Survey Flow randomization method: https://www.qualtrics.com/support/survey-platform/survey-module/survey-flow/standard-elements/randomizer/ This explains the block randomization: https://www.qualtrics.com/support/survey-platform/survey-module/block-options/question-randomization/

  • Author
  • 4 replies
  • February 14, 2020
Thank you for the answer, but I think in this case using the Survey Flow randomization makes everything more confusing, since I don't have only 2 options like in the example that I gave, but quite a few more, and before choosing randomly I would also have to do some filtering, and I would have to repeat this action more than 10 times. Maybe it could also work with survey flow, but since I already know javascript that was way easier. I simply wasn't fully understanding that when you hide a block then the javascript code isn't executed, and that when you set an embedded data variable using javascript, the new value becomes "available" in the next block.

Akdashboard
Level 4 ●●●●
Forum|alt.badge.img+6
  • Level 4 ●●●●
  • 488 replies
  • February 14, 2020
Gotcha. And yup, you got it. JavaScript that is embedded in a question won't trigger until that page loads. If it never loads, it won't trigger.

Leave a Reply