JS code to Create a text set based on Selected Choices converting piped text string to actual value | XM Community
Question

JS code to Create a text set based on Selected Choices converting piped text string to actual value

  • 28 April 2021
  • 5 replies
  • 142 views

Userlevel 5
Badge +11

Hi All,
Please could someone explain why Qualtrics is saying there is an 'unexpected identifier' in this code. I've checked this using the action code task as that highlights errors but it says it's fine.
The purpose of the code is upon submitting the page (clicking next) i want to convert any piped text strings to their actual values and then save it as a embedded field. Doing it this way saves sooooo many branches and setting up extra embedded fields.
For instance, a Multiple Choice question is answered by selecting options 1 and 3 = QC_Food_Venue_2_Name, Coffee Shop
This is how the question answers show up in the Data and analysis section because option 1 is piped from an embedded field.
image.pngI want to convert the embedded data string to its actual value. Here's the code I've prepared. I've tested parts of it using JSFiddle and the parts all seem to work (eg testing my .replace and .startswith code) so I assume I have missed something off that Qualtrics needs but JSFiddle doesn't.
Qualtrics.SurveyEngine.addOnPageSubmit(function(type)
{
var that = this;
if(type == "next") //listens for the next button
{
var arr = that.getSelectedChoices(); //saves the selected choices to a variable
var arr_split = arr.split(","); //splits the selected choices into an array assuming the delimeter is ', '

//cycle through the array and test for an embedded field string.
for(i = 0;i if (arr_split[i].startsWith("${e://Field"))
{
//manipulate the embedded field name to use in the getembeddeddata function and replace the original variable
arr_split[i] =  Qualtrics.SurveyEngine.getEmbeddedData(arr_split[i].replace("${e://Field/","").replace("}",""));
}
}
//save the whole array to an embedded field
Qualtrics.SurveyEngine.setEmbeddedData('Q211_Dining_Lunch_Locations', arr_split);
}
});

Hope someone can help
Also, can I assume that I can set an embeddeddata field in the way I have from an array? Perhaps it will include unwanted double quotes? I need to get this working in Qualtrics obviously to find out but if I can get some guidance on that too then that would be great.
Thanks

Rod Pestell


==UPDATE==
so I spotted the i=0 had a typo so the above code if now fixed but still doesn't work and I think this is because of my lack of knowledge and understand of qualtrics and how you use embedded fields and questions within JavaScript. I played about a bit and found that if I place in the next question:
Qualtrics.SurveyEngine.setEmbeddedData("Q211_Dining_Lunch_Locations","${q://QID211/ChoiceGroup/SelectedChoices}");
This seemed to provide the true value names and write it to the embedded field. That seems terribly easy, have I missed something?!


5 replies

Userlevel 7
Badge +21

.getSelectedChoices()
returns and array. So
arr.split() 
will throw an error.

Userlevel 7
Badge +21

Okay...so this was really interesting to debug.
The problem is that the moment you put a $ inside a string, the system starts looking for piping related variables.
So this part of your code:

startsWith("${e://Field")
is the one creating a problem.
However, you really don't need that, because
.getSelectedChoices()
will give you an array which contains the index (starts from 1) of the choices that have been selected.

Userlevel 7
Badge +21

Qualtircs JS API

Userlevel 5
Badge +11

Hi ahmedA ,

Thanks for taking a look. How did you go about debugging that and could you show me a screen shot identifying it was the $ sign? That will help me learn what to look out for in the future.

Also, as per my update and as you say .getSelectedChoices() will only return an array with index numbers which I could then use to detect if '1' is there, however, I realised using ${q://QID211/ChoiceGroup/SelectedChoices} does seem to provide what I need, the actual names. The only thing I need to work out though is that one of the options will be 'Other' (the 4th index) which involves a text box, so I'll need to replace 'Other' with the Text from the actual text box. I am assuming I can use .replace to do this but just be sure I don't use any $ signs anywhere!!
Any thoughts most welcome.

Also, is this $ issue a problem documented somewhere in Qualtrics?

Thanks

Rod

Userlevel 7
Badge +21

Open up the console and see the error it throws. You'll see that the code appears like this:

startsWith("","")
which is a syntax error. Since I was aware that Qulatrics uses
"${...}"
for piping, I realised that this is where the problem was. I'm not sure if its documented.
You could use the array directly for replacing, as the values won't change even if you randomize the choices.
Alternatively, you could query the class ".q-checked" and see the text around it. The exact element you will need to select will be based on the theme.
${q://QID211/ChoiceGroup/SelectedChoices} will provide you answers on the next page, as this is returned from the sever and won't be updated till you load the next page.

Leave a Reply