Change comma separated list to :: separated list | XM Community
Solved

Change comma separated list to :: separated list


Userlevel 3
Badge +7

Hi All,
I created a custom embedded data field and I want to remove the commas (,) from the separations. I would prefer to update to say "replace , with ::" but I am also amendable to removing the , altogether and programming :: at the end of each question selection.
Here is what I have so far:
image.pngimage.png
This is a multi-select question, and I need to display in a list like this inpatient::board, etc.

icon

Best answer by Deepak 5 December 2022, 18:07

View original

26 replies

Userlevel 7
Badge +36

MaggieGentry
You can include the below code to change the delimiter. Replace the embedded data variable with your embedded data. Include this in the next question from the actual question you need value of. So if you need value of QID5 include this on question which is shown on next page. Also, within survey flow you don't need to keep any value for embedded data leave it empty.
Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
var string = '${q://QID5/ChoiceGroup/SelectedChoices}';
var replacestring= string.replace(/,/g , '::');
Qualtrics.SurveyEngine.setEmbeddedData( 'embeddedDataVariable', replacestring );

});
Hope it helps!

Userlevel 3
Badge +7

Deepak,
I added the javascript on Q6 - the question immediately following Q5 - per this note: Include this in the next question from the actual question you need value of.
However, when including the provided code, here is the output - which is the exact same as pre-JavaScript:

image.png
The first field is the actual question responses, the second is the embedded data field. Since this is a multi- select, I think I have to have embedded data to combine my punches - correct?
Again, I want that embedded data field to be :: not , separated.
Here is that survey flow/embedded data, too - if you need for reference:
image.png

Userlevel 7
Badge +36

https://community.qualtrics.com/XMcommunity/discussion/comment/52827#Comment_52827You have to include the JS on the question which is on next page I am assuming that you have shown Q6 on the same page. Hence the result is not :: seperated. Also keep the embedded data value empty I have mentioned that before as well the JS will fill it

Userlevel 3
Badge +7

Deepak
Unfortunately, no - Q6 is on its own page.
I do have logic built in though - so Q6 does not necessarily show for all responses selected at Q5. Thus, there is a possibility that I go from Q5 (where I want my :: logic) to Q8 and not see Q6.
Is it possible to put this javascript on the closing screen for ALL questions where I want :: (this is not going to be limited to just Q5).
I put logic for Q5 both on Q6 and Q8 (where the logic goes sometimes) - here is the logic I used and this is what I am still getting:

I deleted the embedded data field from the survey flow, too - as requested. However, nothing is happening:
image.png
image.png

Userlevel 7
Badge +36

  1. https://community.qualtrics.com/XMcommunity/discussion/comment/52850#Comment_52850If you want it for all those questions you need to repeat the code multiple times based on the different QID.

  2. Include a hidden question on the next page before the end of the survey at last and include the code for all questions in that question.

  3. Also, did you change the embedded data name to "..Combined" in that code which you have in survey flow?

  4. You cannot include code in End of survey text.

Hope it helps!

Userlevel 3
Badge +7

Deepak -
I'm not following what you want on the survey flow/embedded data.
Should I name the field ${q://QID5/ChoiceGroup/SelectedChoices} and leave the value blank - or can I name the field Roles - Combined - set value now - and set value to ${q://QID5/ChoiceGroup/SelectedChoices}.
I'm unclear what you mean by this: Also, within survey flow you don't need to keep any value for embedded data leave it empty.
I created a hidden question at the end of the survey with this code:
Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
var string = '${q://QID5/ChoiceGroup/SelectedChoices}';
var replacestring= string.replace(/,/g , '::');
Qualtrics.SurveyEngine.setEmbeddedData( 'embeddedDataVariable', replacestring );

});

Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
var string = '${q://QID8/ChoiceGroup/SelectedChoices}';
var replacestring= string.replace(/,/g , '::');
Qualtrics.SurveyEngine.setEmbeddedData( 'embeddedDataVariable', replacestring );

});

Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
var string = '${q://QID9/ChoiceGroup/SelectedChoices}';
var replacestring= string.replace(/,/g , '::');
Qualtrics.SurveyEngine.setEmbeddedData( 'embeddedDataVariable', replacestring );

});

Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
var string = '${q://QID10/ChoiceGroup/SelectedChoices}';
var replacestring= string.replace(/,/g , '::');
Qualtrics.SurveyEngine.setEmbeddedData( 'embeddedDataVariable', replacestring );

});
However, it is still not working - see below - which I think has something to do with my embedded data field confusion:
image.pngimage.png

Userlevel 7
Badge +36

https://community.qualtrics.com/XMcommunity/discussion/comment/52867#Comment_52867You need to change the value in code.
Your code would be:
Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
var string = '${q://QID5/ChoiceGroup/SelectedChoices}';
var replacestring= string.replace(/,/g , '::');
Qualtrics.SurveyEngine.setEmbeddedData( 'Roles - Combined', replacestring );

});

Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
var string = '${q://QID8/ChoiceGroup/SelectedChoices}';
var replacestring= string.replace(/,/g , '::');
Qualtrics.SurveyEngine.setEmbeddedData( 'Roles - Combined', replacestring );

});

Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
var string = '${q://QID9/ChoiceGroup/SelectedChoices}';
var replacestring= string.replace(/,/g , '::');
Qualtrics.SurveyEngine.setEmbeddedData( 'Roles - Combined', replacestring );

});

Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
var string = '${q://QID10/ChoiceGroup/SelectedChoices}';
var replacestring= string.replace(/,/g , '::');
Qualtrics.SurveyEngine.setEmbeddedData( 'Roles - Combined', replacestring );

});
Keep the value empty like this:
image.pngInclude the entire above code in the last empty question as mentioned previously and auto click it to end of survey.
this.clickNextButton();
Hope it helps!

Userlevel 3
Badge +7

That worked!!!
Thank you!
One more question - there is a space after the :: in the embedded data fields.
Is there a way to remove? City of Fishers:: Community Physician Network:: Community Physician Network - All - so it should look like this:
City of Fishers::Community Physician Network::Community Physician Network - All (no spaces between ::)

Userlevel 7
Badge +36

https://community.qualtrics.com/XMcommunity/discussion/comment/52892#Comment_52892You can use the below code:
Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
var string = '${q://QID76/ChoiceGroup/SelectedChoices}';
var new1=string.replace(/\\s*,\\s*/g, ",");
var replacestring= new1.replace(/,/g , '::');
console.log(replace)
console.log(new1)
Qualtrics.SurveyEngine.setEmbeddedData( 'Roles - Combined', replacestring );

});


Hope it helps!

Userlevel 3
Badge +7

So this re-broke the logic:
Here is what I have:
Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
var string = '${q://QID5/ChoiceGroup/SelectedChoices}';
var new1=string.replace(/\\s*,\\s*/g, ",");
var replacestring= new1.replace(/,/g , '::');
console.log(replace)
console.log(new1)
Qualtrics.SurveyEngine.setEmbeddedData( 'Role', replacestring );

});


Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
var string = '${q://QID8/ChoiceGroup/SelectedChoices}';
var new1=string.replace(/\\s*,\\s*/g, ",");
var replacestring= new1.replace(/,/g , '::');
console.log(replace)
console.log(new1)
Qualtrics.SurveyEngine.setEmbeddedData( 'Product Line', replacestring );

});


Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
var string = '${q://QID9/ChoiceGroup/SelectedChoices}';
var new1=string.replace(/\\s*,\\s*/g, ",");
var replacestring= new1.replace(/,/g , '::');
console.log(replace)
console.log(new1)
Qualtrics.SurveyEngine.setEmbeddedData( 'Specialty/Region', replacestring );

});


Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
var string = '${q://QID10/ChoiceGroup/SelectedChoices}';
var new1=string.replace(/\\s*,\\s*/g, ",");
var replacestring= new1.replace(/,/g , '::');
console.log(replace)
console.log(new1)
Qualtrics.SurveyEngine.setEmbeddedData( 'Sub-Specialty/Sub-Region', replacestring );

});
Here is each respective embedded data field:
image.pngHowever, nothing is coming through in my embedded data fields now!
image.png

Userlevel 7
Badge +36

https://community.qualtrics.com/XMcommunity/discussion/comment/52943#Comment_52943That did worked for me not sure where you are facing the issue.
Are you updating that embedded data in survey flow anywhere?

Userlevel 3
Badge +7

Deepak ,
I have this programmed on the last question/a hidden question in the survey:
Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
var string = '${q://QID5/ChoiceGroup/SelectedChoices}';
var new1=string.replace(/\\s*,\\s*/g, ",");
var replacestring= new1.replace(/,/g , '::');
console.log(replace)
console.log(new1)
Qualtrics.SurveyEngine.setEmbeddedData( 'Role', replacestring );

});


Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
var string = '${q://QID8/ChoiceGroup/SelectedChoices}';
var new1=string.replace(/\\s*,\\s*/g, ",");
var replacestring= new1.replace(/,/g , '::');
console.log(replace)
console.log(new1)
Qualtrics.SurveyEngine.setEmbeddedData( 'Product Line', replacestring );

});


Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
var string = '${q://QID9/ChoiceGroup/SelectedChoices}';
var new1=string.replace(/\\s*,\\s*/g, ",");
var replacestring= new1.replace(/,/g , '::');
console.log(replace)
console.log(new1)
Qualtrics.SurveyEngine.setEmbeddedData( 'Specialty/Region', replacestring );

});


Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
var string = '${q://QID10/ChoiceGroup/SelectedChoices}';
var new1=string.replace(/\\s*,\\s*/g, ",");
var replacestring= new1.replace(/,/g , '::');
console.log(replace)
console.log(new1)
Qualtrics.SurveyEngine.setEmbeddedData( 'Sub-Specialty/Sub-Region', replacestring );

});
And I sent you exactly what I was doing with the embedded data. Here is a screenshot for reference, too:
image.png
As mentioned, this worked before we tried to remove the spaces.
Is there something "funky" with my code/something about removing the spaces??

Userlevel 7
Badge +36

https://community.qualtrics.com/XMcommunity/discussion/comment/52956#Comment_52956Change the console.log(replace) to console.log(replacestring) and then check within console if the value is appearing correctly. As the code looks fine to me just change the console piece for each one and check.

Userlevel 3
Badge +7

Nope - didn't work!
Now the old script that WAS WORKING isn't working!
Here is what I used:
Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
var string = '${q://QID5/ChoiceGroup/SelectedChoices}';
var replacestring= string.replace(/,/g , '::');
Qualtrics.SurveyEngine.setEmbeddedData( 'Role', replacestring );

});
Any thoughts? What can we do to fix this!
Is it possible to send you the survey link so you can investigate?

Userlevel 3
Badge +7

I might have just found the issue!
This code can't be on a hidden question - but must be on a "shown question."
When I switched from Hidden Question Type (Timer) to a Text/Placeholder shown question, the "old code" worked - so now time to try the new code!

Userlevel 3
Badge +7

Okay - fixed!
New code with console.log(replacestring) works on a SHOWN question!!!
This is resolved! Thanks for all of your help!!

Userlevel 3
Badge +7

@Deepak - Ran into a quandary. Can you help?

The Javascript is only concatenating the first few (3 to be exact) selections with "::" for multiple different fields. When more than 3 items are selected, there is no “::” only “,”

Example: Three or less selected:

  • PERFECT: City of Fishers::Community Physician Network::Community Physician Network - All::CPN Providers
  • NOT PERFECT (MORE THAN 3 SELECTED): ACO, Ambulatory Pharmacy, Analytics Associate Feedback, Infusion, Inpatient, Inpatient Psych - Fairbanks

Here is my code - any thoughts/what can I do to update this to capture more than 3 selections?:

Qualtrics.SurveyEngine.addOnload(function()
{
    /*Place your JavaScript here to run when the page loads*/
var string = '${q://QID5/ChoiceGroup/SelectedChoices}';
    var new1=string.replace(/\\s*,\\s*/g, ",");
    var replacestring= new1.replace(/,/g , '::');
    Qualtrics.SurveyEngine.setEmbeddedData('Role', replacestring);
    
});


Qualtrics.SurveyEngine.addOnload(function()
{
    /*Place your JavaScript here to run when the page loads*/
var string = '${q://QID50/ChoiceGroup/SelectedChoices}';
    var new1=string.replace(/\\s*,\\s*/g, ",");
    var replacestring= new1.replace(/,/g , '::');
    Qualtrics.SurveyEngine.setEmbeddedData('Product Line', replacestring);
    
});


Qualtrics.SurveyEngine.addOnload(function()
{
    /*Place your JavaScript here to run when the page loads*/
var string = '${q://QID52/ChoiceGroup/SelectedChoices}';
    var new1=string.replace(/\\s*,\\s*/g, ",");
    var replacestring= new1.replace(/,/g , '::');
    Qualtrics.SurveyEngine.setEmbeddedData('Specialty/Region', replacestring);
    
});


Qualtrics.SurveyEngine.addOnload(function()
{
    /*Place your JavaScript here to run when the page loads*/
var string = '${q://QID53/ChoiceGroup/SelectedChoices}';
    var new1=string.replace(/\\s*,\\s*/g, ",");
    var replacestring= new1.replace(/,/g , '::');
    Qualtrics.SurveyEngine.setEmbeddedData('Sub-Specialty/Sub-Region', replacestring);

 

Userlevel 7
Badge +36

@MaggieGentry 

The code does work for more than three as well. Still if you want to try other way give below code a try

Qualtrics.SurveyEngine.addOnload(function() {

var selectedChoices = '${q://QID5/ChoiceGroup/SelectedChoices}';
if (selectedChoices) {

var choicesArray = selectedChoices.split(",");

var replacestring = choicesArray.join("::");
console.log(replacestring)

Qualtrics.SurveyEngine.setEmbeddedData('Role', replacestring);
}
});

Hope it helps!

Userlevel 3
Badge +7

@Deepak - something must be “off” 

Here is my code: 

Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
var string = '${q://QID5/ChoiceGroup/SelectedChoices}';
var new1=string.replace(/\\s*,\\s*/g, ",");
var replacestring= new1.replace(/,/g , '::');
console.log(replacestring)
console.log(new1)
Qualtrics.SurveyEngine.setEmbeddedData( 'Role', replacestring );

});


Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
var string = '${q://QID50/ChoiceGroup/SelectedChoices}';
var new1=string.replace(/\\s*,\\s*/g, ",");
var replacestring= new1.replace(/,/g , '::');
console.log(replacestring)
console.log(new1)
Qualtrics.SurveyEngine.setEmbeddedData( 'Product Line', replacestring );

});


Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
var string = '${q://QID62/ChoiceGroup/SelectedChoices}';
var new1=string.replace(/\\s*,\\s*/g, ",");
var replacestring= new1.replace(/,/g , '::');
console.log(replacestring)
console.log(new1)
Qualtrics.SurveyEngine.setEmbeddedData( 'Specialty/Region', replacestring );

});


Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
var string = '${q://QID53/ChoiceGroup/SelectedChoices}';
var new1=string.replace(/\\s*,\\s*/g, ",");
var replacestring= new1.replace(/,/g , '::');
console.log(replacestring)
console.log(new1)
Qualtrics.SurveyEngine.setEmbeddedData( 'Sub-Specialty/Sub-Region', replacestring );

});

But this is what it is providing:

First is “good” - but only up to 3 - second is “bad”.

How can I expand the first example to contain/work on more than 3?

 

Userlevel 7
Badge +36

@MaggieGentry

The code does work for more than three as well. Still if you want to try other way give below code a try

Qualtrics.SurveyEngine.addOnload(function() {

var selectedChoices = '${q://QID5/ChoiceGroup/SelectedChoices}';
if (selectedChoices) {

var choicesArray = selectedChoices.split(",");

var replacestring = choicesArray.join("::");
console.log(replacestring)

Qualtrics.SurveyEngine.setEmbeddedData('Role', replacestring);
}
});

Hope it helps!

Try using this code.

Userlevel 3
Badge +7

@Deepak,

I identified the issue. The replace string does not work if an option is “ ’s” is selected - such as “Women’s”

 

So this really isn’t a numbers issue, but a “ ‘s” issue. I can eliminate the ‘s - unless you have a workaround?

Userlevel 7
Badge +36

@MaggieGentry

Just replace single quotes with double quotes in each of string variable. So this
var string = '${q://QID53/ChoiceGroup/SelectedChoices}';
will be this:
var string = “${q://QID53/ChoiceGroup/SelectedChoices}”;

Hope it helps!

P.S:  Kindly accept the answer to your original question.

Userlevel 3
Badge +7

@Deepak,

Thanks for the advice! 

I went through and liked every comment in the conversation - there wasn’t an “accept option” that I could find.

Userlevel 7
Badge +20

Hello @MaggieGentry, @Deepak was referring to marking his reply as “Best Answer” which essentially accepts the reply and pins it to the top of this post. I have gone ahead and accepted the original answer for you. Have a great day! 🙂

Userlevel 3
Badge +7

@JoseS  - How did you do that?? - Is it literally a reply message that says “best answer”

Leave a Reply