Is it possible to dynamically change the Question Text based on an embedded field value? | XM Community
Skip to main content

Instead of duplicating a question and adding display logic to display either one depending on whether or not an embedded data field is empty, I'd like to just have javascript dynamically change the question text depending on whether or not the embedded field is empty. I've already proven I can do this by duplicating the question and adding display logic to display either one based on whether or not a person's last name is empty (after setting the embedded data after my authentication piece). I would just rather have a single question in my response output.
So far I have:
Qualtrics.SurveyEngine.addOnload(function()
{
   /*Place your JavaScript here to run when the page loads*/
   var text = jQuery("#"+this.questionId+" .QuestionText")
   var lastname = "${e://Field/LAST_NAME}";
   if(!!lastname)
      text.html("We DONT HAVE your last name")
   Else
      text.html("We have your last name")
});
I've also tried variations on the logic to check for the null value (e.g. lastname === "", lastname is null, etc.), but I can't seem to make it work.

Here's one workaround without using javascript - just more embedded data, and branches in your survey flow.
Say

lastname
is the field with embedded data for your respondent's last name. We can create another embedded data field with the text that you want to appear in your question... we'll call it
lastname_qtext
here.
Add the embedded data for
lastname
in your survey flow. Beneath that, create a branch in your survey flow that respondents will go through with the condition that
lastname
is empty. Within that branch, create a new embedded data field called
lastname_qtext
, and set it to be whatever text you want displayed in the question for respondents missing last names. So it could be something like "DON'T" in this case.
If you want, you can create another branch for the condition that
lastname
is NOT empty.
When writing your question, use piped text for the new embedded data field
lastname_qtext
to create a question that will dynamically change based on the value of
lastname_qtext
(which was determined by the value of
lastname
). You'd just have to make sure that the question comes AFTER the branch in your survey flow where you define
lastname_qtext
.
Here's a screenshot of what your survey flow and question text could look like.
image.png
Here's what the question text could look like, with the piped text.
image.pngHere's what the question would look like when the last name is missing:
image.pngAnd here's what the question text would look like when the last name is present:
image.png
Hope that works for you!


Qualtrics.SurveyEngine.addOnReady(function () {
qc = this.getQuestionContainer().querySelector(".QuestionText");
lastname = "${e://Field/LAST_NAME}";
if (lastname == "") {
qc.innerText = "We DONT HAVE your last name";
} else {
qc.innerText = "We have your last name";
}
});


I didn't just get one working answer, but two! Tested both solutions, and they both work. Thanks!


Leave a Reply