Extracting n left characters of the string in embedded data to piped text in next question | XM Community
Solved

Extracting n left characters of the string in embedded data to piped text in next question

  • 14 October 2020
  • 6 replies
  • 389 views

Hello. I hope everyone is well.
I would like to extract the first 10 characters from an embedded data string and pipe that text into the next question. I have attempted using the suggestion here (https://www.qualtrics.com/community/discussion/3111/left-right-substring-extraction-functions-from-piped-text) as well as others that I have found and can't seem to get anything to work. Below, you will find where I am stuck right now.
My survey is structured in this way:
Block 1

  • Alpha Directory (A-F, G-K, ...)

Block 2
  • List of items under A-F

  • List of items under G-F

  • ...

Block 3
  • Question with piped text from embedded data

In Block 2, the strings in the lists are in this text format: ABCD 0123 | ABC: 01234
In Block 3, I want the piped text from the list selection in Block 2 to be the characters on the left of the "|" character, including the space. The javascript that I have so far looks like this:
Qualtrics.SurveyEngine.addOnload(function()
{

var str = "${q://QID5/SelectedChoices}";
var substr = str.substring(0,10);
Qualtrics.SurveyEngine.setEmbeddedData("subcourse", substr);


});


Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/


});


Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/


});
My Survey Flow looks like this:
image.pngThe piped text question looks like this:
image.pngHowever, the preview window shows this:
image.pngI really am not sure what I am getting wrong and would appreciate your guidance and help. 😊

icon

Best answer by TomG 14 October 2020, 18:31

View original

6 replies

Userlevel 7
Badge +27

You can't set and pipe embedded data on the same page. The most efficient solution would be to pipe the entire string into the question in the third block inside a tag and modify it on the fly.
HTML:
This is a test:${q://QID5/SelectedChoices}
JS:
var subcourse = jQuery("#subcourse");
subcourse.text(subcourse.text().substring(0,10));
Qualtrics.SurveyEngine.setEmbeddedData("subcourse",subcourse.text());
You only need the last line if you need to record subcourse in your response data.

TomG Thank you so much for responding so quickly.
I am new to Qualtrics so it is still unclear where the javascript should go (which block and question?) I just attempted what you described but I am still getting a blank result. Could you provide more details about the placement of a script for this purpose? If there is documentation or a tutorial on this topic, I'd be very interested to learn more.
Thank you for your time and patience with me!

Userlevel 7
Badge +27

https://www.qualtrics.com/community/discussion/comment/31108#Comment_31108The HTML and JS would go with Q9 in your example above. To add the HTML click on the Question Text then the HTML view tab. The JS would go inside the addOnload function (click the JS logo to the left and paste the lines inside the addOnload function).

In Q9, the HTML looks like this:
This is a test: ${q://QID5/SelectedChoices}
The JS looks like this:
Qualtrics.SurveyEngine.addOnload(function()
{

var subcourse = jQuery("#subcourse");
subcourse.text(subcourse.text().substring(0,10));
Qualtrics.SurveyEngine.setEmbeddedData("subcourse",subcourse.text());


});


Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/


});


Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/


});
In the code above, I have also tried this with only lines 1 and 2 (omitting line 3 because I don't actually need it in my response data). The result is still blank.
Is there something terribly obvious I am missing?

Userlevel 7
Badge +27

https://www.qualtrics.com/community/discussion/comment/31117#Comment_31117The code works for me, so I'm guessing that your pipe is wrong and therefore empty. Maybe it is supposed to be ${q://QID5/ChoiceGroup/SelectedChoices}? Anyway, try piping it from the Piped Text tab to make sure you get it right.

That was it! It worked!! Thank you so much.
Just in case anyone else finds this, this is the correct HTML with reference to selected answer. I did as suggested and got the full piped text path, copied it, then added it to the HTML to replace the incorrect path.
This is a test: ${q://QID5/ChoiceGroup/SelectedChoices}

Leave a Reply