Setting a conditional embedded variable with JavaScript | XM Community
Skip to main content

Hi - apologies my question has been discussed before (for instance here: https://community.qualtrics.com/XMcommunity/discussion/13161/setting-embedded-data-using-if-else-statement-in-javascript), but I'm having difficulty following the answers because I have no experience using JavaScript.
I set an embedded variable in the survey flow called "partnerand2". I set this to the string "null". This precedes my question block.
image.pngThen in my block, I have a question, QID344 (ZHPART below). If respondents select yes, I want the value of the embedded variable to be updated to the string "and your partner", so that I can use this as piped text in the questions ahead (for example the next question QID407).
image.pngI added the below two lines of JS code to QID407 to do that after looking at discussions on the XM Community, but failed to get it working. Could you please tell me what's wrong? If I can get this to work, I can merge my next block to my current one and get my back button back.
Qualtrics.SurveyEngine.addOnReady(function()
{ /*Place your JavaScript here to run when the page is fully displayed*/
if("${q://QID344/ChoiceGroup/SelectedChoice}"=="1") 
 {Qualtrics.SurveyEngine.setEmbeddedData('partnerand2', 'and your partner');
 };
Thank you!

Try this instead: 
image.pngPlace this code in the Onload section of your ZHPART question. This will look at the selected choice each time one is clicked and update the embedded data appropriately.


Hello mmoore ! Thank you so much for this! It works now.
I put in your code into the Onload bit of ZHPART, and realized the IF condition always returned FALSE. So it occurred to me that I was using the recode value (1), whereas I should have been using the original value for "Yes", which was 4. So I updated the code to read " if (choiceNum == 4)" and now I can get the embedded variable to update as I wish.


Hello mmoore and Tugba
I am trying to use your code to set a member id for my survey. I my using this survey to collect information about families. When the family is enrolled, they get a family ID and they should get a family member ID. I have already created the family id, but I need to create the member id. My survey today allows a family to register 10 members. I need to create a member id only when a member is registered. I thought about creating an embeded data called id_member1 and fill this embeded data only if the name was not empty. I would do the same for id_member2, id_member3 and so on. Could you help me with a code in js that could work?
Thank you


Leave a Reply