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

ckraemerckraemer GA, USACommunity Member Qubie ✭
edited October 14 in Best Practices

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

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:

The piped text question looks like this:

However, the preview window shows this:

I really am not sure what I am getting wrong and would appreciate your guidance and help. 😊

Best Answers

  • TomGTomG Raleigh, NCCommunity Member Wizard ✭✭✭✭✭
    Accepted Answer

    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 <span> tag and modify it on the fly.

    HTML:

    This is a test:<span id="subcourse">${q://QID5/SelectedChoices}</span>
    

    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.

  • TomGTomG Raleigh, NCCommunity Member Wizard ✭✭✭✭✭
    Accepted Answer

    The 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).

Answers

  • ckraemerckraemer GA, USACommunity Member Qubie ✭

    @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!

  • ckraemerckraemer GA, USACommunity Member Qubie ✭
    edited October 14

    In Q9, the HTML looks like this:

    This is a test: <span id="subcourse">${q://QID5/SelectedChoices}</span>
    

    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?

  • TomGTomG Raleigh, NCCommunity Member Wizard ✭✭✭✭✭
    edited October 14

    The 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.

  • ckraemerckraemer GA, USACommunity Member Qubie ✭

    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: <span id="subcourse">${q://QID5/ChoiceGroup/SelectedChoices}</span>
    
Sign In to Comment