Javascript to filter values from embedded field and then select random item | XM Community

Javascript to filter values from embedded field and then select random item

  • 6 March 2019
  • 2 replies
  • 14 views

Badge +2
Kia ora koutou, hello everyone!

I have an embedded field 'colour_selection' which is populated with answers from a multiple answer questions in the survey flow. I would like to use Javascript to filter out specific answers, and then from the filtered list take a random answer to store back in to an embedded field to pipe in to a following question.

(I would have loved to do this using a loop and merge, but it seems like it's not possible to exclude answers easily).

I have limited Javascript ability, but have managed to get this code to work fine here https://jsfiddle.net/1etL6axn/

However when I try to run this in Qualtrics I'm getting a console error "colourSelection.filter is not a function".

I get an "access denied" error when I try to post code here, so here is a screenshot. And I've attached the qsf.

!


Is this a limitation within Qualtrics? Should I try to find a solution using JQuery instead? Have I missed something unique to Qualtrics to make this work?
Thanks in advance
Dan

2 replies

Userlevel 7
Badge +27
Your issue is that colourSelection is a string, not an array. The first line should be:
```
var colourSelection = "${e://Field/colour_selection}".split(", ");
```
Badge +2
That is exactly what the problem was. Thank you so much for your help Tom.

Leave a Reply