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

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

  • March 6, 2019
  • 2 replies
  • 39 views

Forum|alt.badge.img+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

TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • March 7, 2019
Your issue is that colourSelection is a string, not an array. The first line should be: ``` var colourSelection = "${e://Field/colour_selection}".split(", "); ```

Forum|alt.badge.img+2
  • Author
  • March 7, 2019
That is exactly what the problem was. Thank you so much for your help Tom.