Referencing previous question answer choice for conditional embedded data using javascript | XM Community
Skip to main content

Referencing previous question answer choice for conditional embedded data using javascript

  • August 9, 2022
  • 2 replies
  • 513 views

Forum|alt.badge.img+1

This is a simplification of a problem that I am having that requires javascript.

Let's say I have two questions in the same page:

Q1: Please choose one:

  • Tim

  • Bob

  • New partner

Q2: [in-page display logic: conditional on Q1=="New Partner"] Please type their name: [text entry]

I want to set embedded data if there is new partner. Let's say I put the javascript in Q2. How would I reference Q1 answer choice so that I can say "if Q1=="New Partner" then set Q2 text entry as embedded data"? I understand I can reference the id of the current question by doing this.questionId but how does that work with the previous question or any other question? Or maybe I can condition the javascript on whether Q2 is shown somehow?

2 replies

bgooldfed
Level 4 ●●●●
Forum|alt.badge.img+25
  • Level 4 ●●●●
  • August 10, 2022

Hi adstr_bepp,
Could you not just set the Q2 text entry as embedded data every time (ie; don't check Q1 at all, as it is taken care of via display logic)? If Q2 is empty, it won't write anything to the embedded data.
If not, you should be able to do something like this:
var x = "${q://QID1/ChoiceGroup/SelectedChoices}"; //replace QID1 with relevant question ID

if(x == "New Partner") {
var partner = jQuery('#' + this.questionId).text();
Qualtrics.SurveyEngine.setEmbeddedData('My_Data', partner);
}
May need some tweaking but that's the general gist.
Good luck!


Forum|alt.badge.img+1
  • Author
  • August 12, 2022

https://community.qualtrics.com/XMcommunity/discussion/comment/48371#Comment_48371Hey bgooldfed, thanks for the response! Yes, I could do your first point makes it simpler.