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

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!

@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 (arrri].trim() !== "") {
answerList.push(arrri]);
}
}

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


Add this line before the join:

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

 


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


Leave a Reply