Replacing every other comma in Selected Choices piped text | XM Community
Skip to main content
I am creating a survey on which respondents can select multiple choices from a long list of names. Each possible choice is in the format of "LastName, FirstName." I would like to show respondents a list of their selections for review before submitting the survey.



I created a static content text question using the piped text, ${q://QID2/ChoiceGroup/SelectedChoices}, to display the selected choices. This displays the selected choices as a long comma separated string (i.e., "LastName1, FirstName1, LastName2, FirstName2,...). I tried to custom code a function to replace the commas with line breaks using guidance provided here: https://www.qualtrics.com/community/discussion/198/how-to-change-the-separator-for-piped-text



My issue is that this replaces every comma, so, instead of:



LastName1, FirstName1

LastName2, FirstName2

...



I end up with:



LastName1

FirstName1

LastName2

FirstName2

...



Can anyone provide advice on how to replace every other comma?



Thank you so much for any help you might be able to provide.
Hello @lignecpq ,



Step1: Make a embedded data(answer) in survey flow before the question



Step2: Paste the below code in the multichoice question, "Add Javascript"(OnReady) option.



jQuery(document).on('change', function(){

var ans=[];

jQuery("input:checkbox:checked").each(function(){

ans.push(jQuery(this).parent().find("label > span").text());

});

var s="";

for(var i=0;i<=ans.length-1;i++){

s=s+ans[i]+ '<br>';

}

Qualtrics.SurveyEngine.setEmbeddedData( 'answer',s );

});





Step3: Now pipe embedded data "answer" wherever required
Thank you so much! This worked perfectly for me. I appreciate your time and clear explanation.
Hello, what should I do if I want both selected answers and text entry?

Leave a Reply