Push embedded data to loop and merge field | XM Community
Question

Push embedded data to loop and merge field

  • 28 July 2022
  • 3 replies
  • 695 views

Badge

Hi All,
I have a javascript array of image links saved as an embedded data

"image_links"
. I would like to push each element in the array into a loop and merge field
(Field 1)
.
// to get image links from javascript array
img_links = Qualtrics.SurveyEngine.getEmbeddedData('img_links');
// push each array entry into a loop and merge field "${lm://Field/1}"
for (var num = 0; num < img_links.length; num++) {
// To do
}
The goal is to display the different images in each loop.


3 replies

Userlevel 3
Badge +10

I don't think there's a way to do it in code like that (maybe there is, I haven't heard of it though). If you know the maximum number of elements N_Max (i.e., if there is a hard limit on the number of images you'll loop through), then you could do an (ugly) workaround to get it to work:

  1. Create N embedded variables (im01, im02, etc. Let's pretend N_Max=20 here.)

  2. In your script, set the value of the embedded variable to the value of each row in your array.

  3. Have a hidden checkbox question where you pipe im01...im20 into the answer choices.

  4. Use javascript on the hidden question to select (check) only the answers that aren't empty.

  5. In your loop and merge, pull the field from a previous question answer, select your hidden question, and select to loop through only selected answers.

Badge

So I was able to find a simple solution - very similar to your suggestion. I agree, it doesn't seem it can be done in code only.

  • Created N embedded variables
     (e.g., img0, img1,...img19)

  •  Set the value of the embedded variable to the value of each row in the array

Qualtrics.SurveyEngine.setEmbeddedData('img'+num.toString(), img_links[num]);

  • Create a text/graphic question type in a loop and merge block

  • Pipe each embedded variable containing the image url in the loop and merge field
    (Field 1)
    . Example, the first row will contain
    ${e://Field/img0}.

  • In the HTML View of the text/graphic question, embed the image using the html image element


Userlevel 3
Badge +10

Yes, your solution works (and is the simpler method) as long as the array size is constant.
My solution is more complicated only because it allows for a different number of images (and thus a different number of loop/merge iterations) that does not need to be set ahead of time. If the array is size 12, e.g., it will only pull

 img0,...,img11
into the loop/merge.

Leave a Reply