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

Passing a javascript generated answer to the next question

  • January 16, 2023
  • 14 replies
  • 650 views

Forum|alt.badge.img+4

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 = ['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

14 replies

Deepak
QPN Level 8 ●●●●●●●●
Forum|alt.badge.img+46
  • January 17, 2023

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!


Forum|alt.badge.img+4
  • Author
  • Level 1 ●
  • January 17, 2023

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?


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • January 17, 2023

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];


Deepak
QPN Level 8 ●●●●●●●●
Forum|alt.badge.img+46
  • January 17, 2023

Forum|alt.badge.img+4
  • Author
  • Level 1 ●
  • January 17, 2023

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.


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • January 17, 2023

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.


Forum|alt.badge.img+4
  • Author
  • Level 1 ●
  • January 17, 2023

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.


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • January 17, 2023

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]);
});


Forum|alt.badge.img+4
  • Author
  • Level 1 ●
  • January 17, 2023

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!


Forum|alt.badge.img+4
  • Author
  • Level 1 ●
  • January 20, 2023

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(locations[loop-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


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • January 20, 2023

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


Forum|alt.badge.img+4
  • Author
  • Level 1 ●
  • January 20, 2023

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.


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • January 20, 2023

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.


Forum|alt.badge.img+4
  • Author
  • Level 1 ●
  • January 20, 2023

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)