Hi all,
In my survey I want have a piece of text in Q2 depend on what was entered in Q1. In this case Q1 is a text field, with possibly 100 different answers (I also need an else clause). Can I use Javascript to use if-else statements, or maybe something with an array?
Conceptually, this is what I want:
if Q1 = "answer text 1" then Q2 = "What is your experience in working with Mary?"
if Q1 = "answer text 2" then Q2 = "What is your experience in working with John?"
...
if Q1 = "answer text 100" then Q2 = "What is your experience in working with Angela?"
else Q2 = "Not sure yet what to ask here..."
The name will also come back in other questions (e.g. Q3). I have a feeling that once Q1 is known, I have to look up the name and then put it in a hidden question. That would make piping in later questions easy.
But how can I nicely look up the name from a long list / array?
Thanks in advance for your insights!
Javascript for piped text, many options
Best answer by ahmedA
You can use this JS in Q2 to achieve what you have described:
Qualtrics.SurveyEngine.addOnReady(function () {
let match_object = {
"answer text 1": "Mary",
"answer text 2": "John",
"answer text 100": "Angela",
};
// You'll need to get the text entry value from Q1 using piped text
let q1Answer = "${q://QID1/ChoiceTextEntryValue}";
// Empty in case of an invalid entry. You can change this
let great_name = match_object[q1Answer] ? match_object[q1Answer] : " ";
// Create embedded data "great_name" which can be used later also
Qualtrics.SurveyEngine.setEmbeddedData("great_name", great_name);
let q2Text = great_name.trim()
? "What is your experience in working with " + great_name
: "Not sure yet what to ask here..."; // Change the value here for the else clause
// This will change the entire question text. If you only want to change a part of it,
// Then use the appropriate selectors. Read up on HTML selectors for this.
this.questionContainer.querySelector(".QuestionText").innerText = q2Text;
});
While the code will work, it will be extremely sensitive, so an extra punction or space anywhere in the first answer will result in an error.
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.
