Capture and embed data within a loop | XM Community
Skip to main content
Solved

Capture and embed data within a loop

  • 23 February 2022
  • 16 replies
  • 881 views

Hello! I've been wrestling with this problem for a while, but due to my inexperience with Javascript, I haven't got too far. I apologise in advance for the hot mess of a code down below!!
My goal is to capture the answer of a question within a loop, each time the loop occurs, and create an embedded data field.
Currently, I'm adding this to the end of the javascript terminal.
Qualtrics.SurveyEngine.addOnPageSubmit(function() {
var current_loop = ${lm://CurrentLoopNumber};
var answer = $(this.QID44).select('.InputText');
var answer_instance= ("Intention" + current_loop)
{   
Qualtrics.SurveyEngine.setEmbeddedData(answer_instance, answer);
}
});

I think my issues are:

  • ${lm://CurrentLoopNumber} isn't an appropriate way to reference the current loop number?

  • $(this.QID44).select('.InputText') won't collect the answer from QID44

  • In general, the javascript terminal says theres an excess '{', and won't allow me to save and quit! Can you spot it?

  • Another thing is that the text entry for this question (QID44) is piped text default text, which I then hide from the participant. Not sure if this affects anything.

Would love some help on this, if anyone has any insight 🙂 I can give more context about the project, if needed. Thank you!!
(More about my loop and merge set up, if you need it to understand why I'm in this mess.
I ask my participants initially to submit up to 30 free-text answers in a matrix table.
I then randomly loop through the same question set for just 10 of these answers.
Three days later, my participants come back, to give more details about their answers, so I need to pipe their original answers into this later survey using embedded data fields.
Let me know if anything/nothing makes sense...)

16 replies

Userlevel 4
Badge +16

Hi there,

the problem is the QID44.

my code would look like this:

Qualtrics.SurveyEngine.addOnPageSubmit(function()
{
let loopNumber = "${lm://CurrentLoopNumber}"
let qId = this.questionId
let embeddedDataFieldname = "textEntry_"
embeddedDataFieldname = embeddedDataFieldname +loopNumber

console.log('QR~'+qId)
let text = document.getElementById('QR~'+qId).value

Qualtrics.SurveyEngine.setEmbeddedData(embeddedDataFieldname, text);
});

in your case the $('#QR~'+this.questionId) might do the trick. the code above works though and I rather do things with plain JavaScript.

Hope this helps

Best regards

Rudi


Badge +1

https://community.qualtrics.com/XMcommunity/discussion/comment/43975#Comment_43975Thank you so much, this worked perfectly. 🙇‍♀️
I have another question... could this code be adapted to extract the selected answer to a multiple choice question, rather than free text? The multiple choice question only allows one answer, but there may be later questions that accept more than one.
Perhaps this line: let text = document.getElementById('QR~'+qId).value
would become: let choice = form.getElementById('QR~'+qId).value ?
Also, do you know any good resources/libraries etc where I can find these basic javascript functions and how to use them? So I don't have to cry for help all the time!

Userlevel 4
Badge +16

Hi there,
the code for this depends on a few things:
is it a single or mutiple choice question
would you like to store the choideId or the recodeValues or the label text?

Please let me know what you want to achieve and I'll try to help

Unfortunately, I don't have any other source but my brain and my knowledge of JS and Qualtrics 😁
Best regards

Rudi

Userlevel 4
Badge +16

and here the code for the single answer multi select

Qualtrics.SurveyEngine.addOnPageSubmit(function()
{
let loopNumber = "${lm://CurrentLoopNumber}"
let qId = this.questionId
let embeddedDataFieldname = "selectedAnswerLabel_"

embeddedDataFieldname = embeddedDataFieldname +loopNumber



let selectedChoice = this.getSelectedChoices()
console.log(qId+'-'+selectedChoice+'-label')


let selectedAnswerLabel = document.getElementById(qId+'-'+selectedChoice+'-label').innerText

Qualtrics.SurveyEngine.setEmbeddedData(embeddedDataFieldname,selectedAnswerLabel);


});

Badge +1

Thanks for the solution to a single select multi option! Trying it now.

Badge +1

Hi Rudi,
Thanks for your rapid response, and for your brain!!
To answer your questions:

  • In one question, there are multiple check boxes to select from, but they can only select one choice. In this one, I think I want to store the label text? I want to remind participants what they chose last time.

  • In another question (less important, and could miss), they could select multiple options, and give free text entry to support the choice they made. Here, I would like to record both the label text of the options (which they chose), and the free text entry, if submitted.

I assume that ChoiceID refers to the number of the choice (first, second, third?), while recodeValues I'm less sure about. Sorry for being so clueless, and thanks again!
Ava

Userlevel 4
Badge +16

Hi there sorry but somehow I don't understand the requirements for the last part where you want to record both options.. are these allow text entry boxes?

Generally speaking choiceIds are Qualtrics internal ids for the answer options, whereas recodes can be set by the user :-)
for example: if you have five answer options in the first place and add an answer option later right after the first option it will still have the choiceId 6 however you could still recode it as 2


Best regards

Rudi


Badge +1

https://community.qualtrics.com/XMcommunity/discussion/comment/43984#Comment_43984Sorry, I wasn't clear! yep, they are 'allow text entry' boxes. I tried to attach a screenshot but it messed up.
Thanks for the explanation of InternalIDs and recodeValues - makes perfect sense.

https://community.qualtrics.com/XMcommunity/discussion/comment/43982#Comment_43982This also works perfectly, thank you! You have made my day!

Badge +1

https://community.qualtrics.com/XMcommunity/discussion/comment/43975#Comment_43975Thanks for your answer Rudi.
In my task, I would like to know how many times people click an audio file, so I count the clicks in Javascript, and I need the Java script knows which loop number presented to participants (that the audio file in) and put the value of the clicks in the embedded data. Then I changed the code to
Qualtrics.SurveyEngine.addOnPageSubmit(function()

{

let loopNumber = "${lm://CurrentLoopNumber}"

let qId = this.questionId

let embeddedDataFieldname = "textEntry_"

embeddedDataFieldname = embeddedDataFieldname +loopNumber

console.log('_playCount'+qId)

let text = document.getElementById('_playCount'+qId).value

Qualtrics.SurveyEngine.setEmbeddedData(embeddedDataFieldname, text);

});


However, I have another problem here. When I ran the code, the console showed me a warning that
SE API Error: TypeError: Cannot read properties of null (reading 'value')
and the code show let loopNumber = "1".
Screen Shot 2022-04-06 at 1.17.15 AM.png
I am not sure whether it is because I randomized my questions in the loop? Do you have any ideas on how to solve this problem?

Kind regard,

Yue

Userlevel 4
Badge +16

https://community.qualtrics.com/XMcommunity/discussion/comment/45205#Comment_45205Hi Yue,
I am not quite sure what the respondents click while listening to an audio file. However
catching click events will work only on addOnReady
in your case - having randomized the loop order - you better catch the question Id and split the string in your loop
question_id = this.questionId // in a loop this will return 1_QID1, 2_QID1,... 5_QID1
loopId = questionid.split('_')[0]
then add the loopid to your embedded datafield

let embeddedDataFieldName = "clickCount_" + loopId

And maybe it would be good to start a new discussion. Others will have it easier to find your post and answer 😉
hope this helps you

Best regards

Rudi


Badge +1

https://community.qualtrics.com/XMcommunity/discussion/comment/45221#Comment_45221Thanks Rudi,
I post a new discussion about my case. https://community.qualtrics.com/XMcommunity/discussion/20428/capture-embeded-click-counts-of-an-audio-file-within-a-randomised-loop/p1?new=1

Badge +1

Rudi
Thanks so much for your help on the previous question; so helpful I am back for more, sorry!
I'd like to do a similar thing, but select the values of the text entry box in a multiple choice question. They can choose as many options to the question as possible.
The code I've tried so far is below -- any idea where I may be going wrong?
Thanks so much!
Qualtrics.SurveyEngine.addOnPageSubmit(function()
{
let loopNumber = "${lm://CurrentLoopNumber}"
let qId = this.questionId
let embeddedDataFieldname = "I"
let reminderLabel = "Reminder"
embeddedDataFieldname = embeddedDataFieldname +loopNumber + reminderLabel
let selectedChoice = this.getSelectedChoices()
console.log(qId+'-'+selectedChoice+'-label')
let selectedAnswerLabel = document.getElementById(qId+'-'+selectedChoice+'-label').value
Qualtrics.SurveyEngine.setEmbeddedData(embeddedDataFieldname,selectedAnswerLabel);
});

Userlevel 4
Badge +16

https://community.qualtrics.com/XMcommunity/discussion/comment/45817#Comment_45817Hi a_e_scott,

sorry for getting back to you on this rather late. But I was partly out of the office and for the rest rather busy.
If I understood you correctly you have a loop where you have a text entry question. the answers of the text entry question should become the labels of a multi-select question outside of the loop.

this can be achieved in a few ways and depends on the way you have setup the loop and merge block.

  1. you need to create as many embedded data fields as you have loop iterations





dd the below javaScipt to the opentext question

.
embedded data fields : make sure they are defined before your loop& merge block

Userlevel 4
Badge +16

Hi a_e_scott,

sorry for getting back to you on this rather late. But I was partly out of the office and for the rest rather busy.
If I understood you correctly you have a loop where you have a text entry question. the answers of the text entry question should become the labels of a multi-select question outside of the loop.

this can be achieved in a few ways and depends on the way you have setup the loop and merge block.

  1. you need to create as many embedded data fields as you have loop iterations

  2. add the below javaScipt to the opentext question

  3. create the multi select question outside the loop & merge block

  4. pipe the text to the answer options

  5. add a display logic to the answer options if textEntry_1 is not empty ....

  6. enjoy the result


1. embedded data fields : make sure they are defined before your loop& merge block
User:

3. multi select question
image.png
4 . display logic
User:
JS code comes in the next comment because I can't insert it here :-(
Hope this helps
Best regard
Rudi

Userlevel 4
Badge +16

a_e_scott
and here the JS code for the text entry question:

Qualtrics.SurveyEngine.addOnPageSubmit(function()
{

let qId = this.questionId
let loopId = qId.split("_")[0] // this is the better option than the one with loopNumber as you can also randomize the items and you still know where this belongs to
let embeddedDataFieldname = "textEntry_"

embeddedDataFieldname = embeddedDataFieldname + loopId


console.log('QR~'+qId)
let text = document.getElementById('QR~'+qId).value

Qualtrics.SurveyEngine.setEmbeddedData(embeddedDataFieldname, text);
});
Best regards

Rudi

Badge +1

How can this code show the selected dropdown text instead of the Question ID?

Leave a Reply