How to pull selected choices into Javascript | XM Community
Skip to main content
Question

How to pull selected choices into Javascript


Forum|alt.badge.img

I am trying to pull selected choices from a multiple choice question that allows multiple selections into JavaScript. I then want to parse the choices to get each selection individually. However, when I use the suggested code for pulling selected choices into JavaScript, I am not getting any result. I have tried multiple ways of referencing selected choices, but I am still coming up with an empty array.
Qualtrics.SurveyEngine.addOnPageSubmit(function () {
var SelectedChoices = this.getSelectedChoices();
  var Schools = SelectedChoices.split(', ');
var School1 = Schools[0];
var School2 = Schools[1];
var School3 = Schools[2];
Qualtrics.SurveyEngine.setEmbeddedData('School1',School1);
Qualtrics.SurveyEngine.setEmbeddedData( 'School2',School2);
Qualtrics.SurveyEngine.setEmbeddedData( 'School3',School3);
});
Thanks in advance for any ideas on what I am doing incorrectly.

4 replies

TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 5911 replies
  • April 21, 2022

getSelectedChoices() returns an array of choice ids.


Forum|alt.badge.img
  • Author
  • 1 reply
  • April 21, 2022

That array appears to be empty though...I would expect to see the numeric id of the choice, right? I'm not getting that. Also, is there a way to get the text value of the choice instead of the ID?


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 5911 replies
  • April 22, 2022

https://community.qualtrics.com/XMcommunity/discussion/comment/45705#Comment_45705It shouldn’t be empty if you've answered the question. To confirm, use this:
console.log(this.getSelectedChoices());
Use the browser’s Inspect feature to check the console. You'll see it is an array with the choice ids.
As for getting the text, there isn't a Qualtrics function for that. You could recode the question and get the variable name (which is the label text by default) by looping through the selected choices and using getChoiceVariableName(). Alternatively (what I would do), you can find and get the label text for the selected choices directly from the DOM.
jQuery("#"+this.questionId+" input:checked").each(function(i) {
Qualtrics.SurveyEngine.setEmbeddedData("School"+(i+1),
jQuery(this).closest("li").find(".LabelWrapper>label>span").text());
});


Forum|alt.badge.img+1
  • 9 replies
  • September 22, 2022

jmaddox TomG
did you get this sorted in the end? and working for multiple choice question repopulation?

I have tried the solution above to no success, I have posted some issues identified on this new post
https://community.qualtrics.com/XMcommunity/discussion/22392/how-do-you-get-the-embedded-data-from-coded-questions-to-populate-a-follow-up-survey#latest


Leave a Reply