Dear Community,
In my survey, I want to present a series of sentences. I'd like these sentences to be constructed by words that are randomized from predetermined lists.
Example of a sentence: [Randy] is a [male] [doctor] experiencing [moderate] [sadness] [often], [moderate] [irritability] [almost always], and [severe] [lack of interest] [almost always].
The parts in the brackets are words that would be randomized from predetermined word lists.
To clarify, the name/word "Randy" would be randomly pulled from a list of names while the word "doctor" would be randomly pulled from a DIFFERENT list of jobs. The same goes for the other words.
Is there any way to do it? I would really appreciate your help!
You'll need to use JavaScript and edit question HTML for this. The following JS demonstrates it for two items being randomly selected. You can make the relevant changes:
Qualtrics.SurveyEngine.addOnReady(function () {
// This is for two fields, you can add as my fields as you require using the same format
// Just make sure you give different variable names
nms = "${e://Field/Names}";
nms = nms.split(",");
r1 = Math.floor(Math.random() * nms.length);
frts = "${e://Field/Fruits}";
frts = frts.split(",");
r2 = Math.floor(Math.random() * frts.length);
// Build the sentence to be used. Everything "inside quotes" is static
sentence = nmshr1].trim() + " is eating " + frtstr2].trim();
// Insert this sentence into the question
document.querySelector("#mysentence").innerText = sentence;
});
See here for the demo and the question HTML used. Each time you click restart survey, the text should be different. Since the options are few, you'll see repetitions.
Thank you so much for your answer! I will definitely try it out!
One more thing, if you don't mind- In the example, the words/emotions "sadness", "irritability", and "lack of interest" come from the same list. If "sadness" is randomly pulled for the first part of the sentence, then I would like for it the be excluded the next time that a word is randomized from that list so that it doesn't repeat itself and another word can take its place (e.g., irritability or lack of interest).
What code is needed to make this adjustment?
Thank you again!
This is the revised code for achieving that. I've also updated the demo here to reflect the change. You'll see the that the same name is never repeated.
Qualtrics.SurveyEngine.addOnReady(function () {
// This is for two fields, you can add as my fields as you require using the same format
// Just make sure you give different variable names
nms = "${e://Field/Names}";
r1 = Math.floor(Math.random() * nms.length);
// Store the selected named and remove it from the list
name1 = nmscr1].trim();
nms.splice(r1, 1);
// Regenerate Random number for the second name, otherwise you'll always get the same combination
r1 = Math.floor(Math.random() * nms.length);
name2 = nmstr1].trim();
frts = "${e://Field/Fruits}";
r2 = Math.floor(Math.random() * frts.length);
// Build the sentence to be used. Everything "inside quotes" is static
sentence = name1 + " and " + name2 + " are eating " + frtstr2].trim();
// Insert this sentence into the question
document.querySelector("#mysentence").innerText = sentence;
});
Hi again!
I have adjusted the code and it seems to be working! I just have a final question if you don't mind -- I would like for the randomized questions to be saved as I need them to run my analysis. How should I go about this?
Thank you!!
Add this line at the end of the code:
Qualtrics.SurveyEngine.setEmbeddedData("Q1_text", sentence);
You'll need to create a different embedded variable for each sentence, so you'll have to change to Q2_text, Q3_text....and so on. You'll also need to create these variables in the survey flow, otherwise it won't show up in the data you export.
Hello again,
In the survey, I have 20 questions that are presented with the use of the same Javascript code (thanks again for that!). I used the function: “Qualtrics.SurveyEngine.setEmbeddedData” in order to save the embedded data in each question. But in the output file, it is only showing the embedded data for the last question/response. I believe I need to find a way for the qualtrics output to generate new columns for the embedded data so that the amount of columns corresponds to the amount of questions/responses. Is there any way to do it?
I would really appreciate your help again as the study is almost ready to be launched!
There could be two reasons for this issue:
- You are not creating embedded variables for each question, so its being overwritten and only the last one is visible. For each question, create an embedded data variable like q1_text, q2_text, q3_text etc.
- These embedded variables are not defined in the survey flow, so even though they function, they aren't visible in the downloaded data. For this, just create embedded variables in the survey flow with the names q1_text, q2_text etc. and leave them empty. Now you should see their values being available in the downloaded dataset.
https://community.qualtrics.com/XMcommunity/discussion/comment/33038#Comment_33038Hi, thanks for your helpful response!
What I don't understand from this is where the list of words is stored in this code? Where does this code pull data from?
Leave a Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.