Add commas to Piped Text from Single Select answer choices | XM Community
Skip to main content
Solved

Add commas to Piped Text from Single Select answer choices

  • October 11, 2023
  • 3 replies
  • 126 views

Forum|alt.badge.img+1

Hello,

I’m trying to get piped answers from previous single select questions to show up as follows:

“PipedAnswer1, PipedAnswer2, PipedAnswer3,...”

Currently how it is set up, this is how it looks like:

“PipedAnswer1 PipedAnswer2, PipedAnswer3...”

 

I need to be able to have the commas show up consistently, but only if a piped answer was selected. I currently have it set up as an array in JS like this below:

And in the HTML of the question text, I have this entered: <div id=”pipe”>QIDs</div>

 

If I was to select PipedAnswer1, PipedAnswer3, and PipedAnswer5, it would look like this:
PipedAnswer, , PipedAnswer3, , PipedAnswer5, , , 

 

How do I get rid of the unneeded commas if the answers aren’t selected above? Do I need some kind of loop? Thanks in advance!

Best answer by Nam Nguyen

@tjdavid Just add a step that skim out all the empty choice

var answerList = [];
// skim out empty choice
for (var i = 0; i < arr.length; i++) {
if (arr[i].trim() !== "") {
answerList.push(arr[i]);
}
}

Then jQuery(“#pipe”).html(answerList.join(“, ”));

3 replies

Nam Nguyen
QPN Level 8 ●●●●●●●●
Forum|alt.badge.img+29
  • QPN Level 8 ●●●●●●●●
  • 1096 replies
  • Answer
  • October 11, 2023

@tjdavid Just add a step that skim out all the empty choice

var answerList = [];
// skim out empty choice
for (var i = 0; i < arr.length; i++) {
if (arr[i].trim() !== "") {
answerList.push(arr[i]);
}
}

Then jQuery(“#pipe”).html(answerList.join(“, ”));


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 6084 replies
  • October 11, 2023

Add this line before the join:

arr = arr.filter(function(string) { return string.trim() != ''; });

 


Forum|alt.badge.img+1
  • Author
  • 1 reply
  • October 11, 2023

Thank you both! I tried both out of curiosity and they both accomplished what I was setting out to do.