Passing a javascript generated answer to the next question | XM Community
Skip to main content

In my survey I have a google map with locations that display in around a users home (from a city they submit). I'm now trying to pass the selected locations (an array with up to 3 strings representing each location) onto the next question in the survey, but I don't know how to do that.
so I literally have the following code:
Qualtrics.SurveyEngine.addOnload(function(){
  //this is the user's city entered in the previous question
var city = jQuery("#"+this.questionId+" .city");
  //first - call API to trigger table...
  var locations = getapi(city.text());
});

here locations is the array output by the users
locations = o'Gino's Pizza','Tom's Diner','Lucy's Tacaria']; etc...
with the locations returned I want to as the same set of questions about each location.
But how?

cheers

https://community.qualtrics.com/XMcommunity/discussion/23574/passing-a-javascript-generated-answer-to-the-next-questionYou can separate the array and push them individually in different embedded data. Create a block of question you want to ask for all and include loop and merge on those embedded data values within block.
Hope it helps!


https://community.qualtrics.com/XMcommunity/discussion/comment/54090#Comment_54090Sorry, I'm not sure how to push them individually to embedded data? Would you be able to provide a short example?


jmcmummey,
You can access the items in the array using the current loop number within the loop. For example:
var loop = Number("${lm://CurrentLoopNumber}");
var location = locations[loop-1];


https://community.qualtrics.com/XMcommunity/discussion/comment/54110#Comment_54110You can use locations[0] for the first and so on.


https://community.qualtrics.com/XMcommunity/discussion/comment/54111#Comment_54111Thanks TomG , but how do I set up the loop if I don't know how many elements will be in the array? It can be anywhere between 1-3 locations. Based on Deepak 's answer, I am thinking I can set up 3 Embeded value locations with a default value of Null, then populate those within the OnUnload()? but I Haven't figured out how to set up a loop for this type of output.


https://community.qualtrics.com/XMcommunity/discussion/comment/54120#Comment_54120Add a text entry question before the loop. Use JS answer with the number of locations and hide the question. Base your loop off the answer to that question.


https://community.qualtrics.com/XMcommunity/discussion/comment/54111#Comment_54111TomG Thanks,that's a good idea. So the workflow is:
set up 3 embedded data locations like this:
Screenshot 2023-01-17 at 1.50.32 PM.pngThen have my question that contacts my google maps api and writes the answers to the embedded data locations.
then use display logic to hide the next question
Screenshot 2023-01-17 at 1.55.31 PM.pngbut in the OnLoad portion of the javascript api for that question set

${q://QID32/ChoiceTextEntryValue} == 3
(or however many of the embedded data fields are non-null
Is that right?
I feel like I should be using Qualtrics.SurveyEngine.? to set the question text field.


I
https://community.qualtrics.com/XMcommunity/discussion/comment/54125#Comment_54125I was thinking you only need one embedded data field - a delimited list of the locations.
Base the loop off a numeric entry to a text entry question. Parse your delimited list to find the number of locations and that becomes your answer.
Within your loop, parse your delimited list and display the location based on the loop index. Use JS to do the 'pipe'. Let's say 'locations' is your delimited embedded data field. You could have this html wherever you want to pipe location:

Then JS something like:
Qualtrics.SurveyEngine.addOnload(function() {
var loop = Number("${lm://CurrentLoopNumber}");
var locations = "${e://Field/locations}".split(",");
jQuery("#"+this.questionId+" .location").text(locations[loop-1]);
});


alright awesome. I couldn't figure out how to hide the text input using display logic, but I've managed to do it using:

jQuery(":input").css("display","none");

and setting the default value from an embedded data point.
Thanks so much!


https://community.qualtrics.com/XMcommunity/discussion/comment/54129#Comment_54129Hey, Tom,
I thought I had this solved, but I'm seeing a strange behaviour when I try your code. I'm piping in the EmbeddedData into question and using the following code
var loop = Number("${lm://CurrentLoopNumber}");
var locations = "${e://Field/locations}".Split('\\t');
console.log(loop,locations[loop-1])
   jQuery("#"+this.questionId+" .location").text(locationsnloop-1]);
However for the first loop question locations (and the displayed text) is 'null'. The 2nd loop question displays appropriately, and if I hit the back arrow the first loop question now displays appropriately. Any suggestion on what's happening?

Regards,
Jesse


https://community.qualtrics.com/XMcommunity/discussion/comment/54307#Comment_54307Split should be split (lowercase). It should have thrown a console error.


https://community.qualtrics.com/XMcommunity/discussion/comment/54308#Comment_54308yes, sorry, that was a typo, functionality is still the same though.
Actually, I have a workaround, I've added a question in between the one where I get the locations and the loop. That seems to give the survey engine time to update the embedded data. Not ideal, but I can work with that.


https://community.qualtrics.com/XMcommunity/discussion/comment/54311#Comment_54311That's how embedded data works. You can't set a field then pipe it on the same page. If you need to do on the same page, you need to get the locations directly from the page using JS.


https://community.qualtrics.com/XMcommunity/discussion/comment/54314#Comment_54314No, I get the data via javascript and set it to embedded data.
I was then piping the embedded data into a separate block, but that was causing errors, so I've had to add an additional question/page to allow the embedded data to load.
Page 1: JS() -> embedded Data
Page 2 - embeddedData-> loop (fails)

Page 1: JS() -> embedded Data
Page 2: do nothing
Page 3: embeddedData -> loop (works)


Leave a Reply