Setting embedded data using javascript | XM Community
Skip to main content
I've read through every post on this topic but I still can't seem to get this to work. I want to capture an answer from a matrix dropdown list into embedded data. Then I want to show that response in a matrix text box in a later block. However, we're also giving the respondent the option to go back and change their answer.



I've tried this without JS by setting the answer as embedded in the survey flow, then piping it in the other block. But when the respondent goes back to change their answer, the piped answer doesn't update in the other block.



So I'm trying to use JS to capture the answer, but the answer is not capturing. I have this code in question JS:

Qualtrics.SurveyEngine.setEmbeddedData('User_answer', currentResponse);

Qualtrics.SurveyEngine.getEmbeddedData('User_answer');



Then I added 'User_answer' as an embedded data element at the very top of my survey flow.



What am I missing?
You first have to assign the current answer to user_answer and than second line of your code will work.



Also please check by writing alert()
How do I do that? Also, I didn't understand your second sentence (I'm still new at this). Thanks in advance!!
Hello @Julie_130 ,



Step 1: Create Embedded data in the survey flow before matrix drop down list question.

The number of embedded data depends on the number of statements you have in matrix drop down list question. For eg if you have 3 statement then make Answer1, Answer2, Answer3 embedded data.



Step 2: Paste the following code in the `js(OnReady)` of the matrix drop down list question



jQuery("select").on('click', function(){

Qualtrics.SurveyEngine.setEmbeddedData('Answer1',jQuery("select:eq(0) :selected").text());

Qualtrics.SurveyEngine.setEmbeddedData('Answer2',jQuery("select:eq(1) :selected").text());

Qualtrics.SurveyEngine.setEmbeddedData('Answer3',jQuery("select:eq(2) :selected").text());

});



Step 3:Pipe in the embedded data(Answer1, Answer2, Answer3) wherever required, in your case it will be in a matrix text box and you will do this while designing the question.
I believe you should call the function on "change" as well since click may not work in every browser because based on theme sometime the actual dropdown is embedded within Label field.



jQuery("select").on('click change', function(){
@Shashi @Mohammedali_Rajapakar_Ugam

Thank you for the suggestions. The JS is working to record the answers in embedded data and pipe into a later question. However, the value still doesn't update if the respondent changes their answer. I did include the 'click change' function as well (see below).



jQuery("select").on('click', function(){

Qualtrics.SurveyEngine.setEmbeddedData('Coach_1',jQuery("select:eq(0) :selected").text());

});

jQuery("select").on('change', function(){

Qualtrics.SurveyEngine.setEmbeddedData('Coach_1',jQuery("select:eq(0) :selected").text());

});



Any ideas on how to force the update in the piped text? I know this will work if using a descriptive text question, but we need the editable boxes so the respondent can manually change any "Other" answers to an actual name.

Thoughts?
1. Add the following code in the `js(OnReady)` of the matrix drop down list question:



jQuery("select").on('click change', function(){

Qualtrics.SurveyEngine.setEmbeddedData('Answer1',jQuery("select:eq(0) :selected").text());

Qualtrics.SurveyEngine.setEmbeddedData('Answer2',jQuery("select:eq(1) :selected").text());

Qualtrics.SurveyEngine.setEmbeddedData('Answer3',jQuery("select:eq(2) :selected").text());

});
2. Add the following code in the js(OnReady) of the matrix table text entry question

` jQuery("[id='QR~"+this.questionId+"~1~1~TEXT']").val("${e://Field/Answer1}");`



In the above code the answer is piped in 1st row and 1st column text entry, to change the text entry box just insert proper row and column number in the above code(QR~"+this.questionId+"~ROW~COLUMN~TEXT)
That's perfect!!! Thank you!!!!

Leave a Reply