How do I add piped text using JavaScript? | XM Community
Skip to main content

This is part of a bigger question - but this is essentially where I'm stuck: Inputting a piped text into a question using javascript. It works if I manually type out the piped text on the question - but I can't do this because I use javascript to identify which question text needs to be displayed. Both of these that I tried do not work - ie. blank output and the code is onload on the previous qsn.
image.pngIf it helps: the data in q://QID40/QuestionText looks like this (with the fields filled with embedded elements):
image.pngAny help would be great thank you so much!

Hi,

I am not quite sure why you want to add the text using JavaScript.
If I understand you correctly you have the text already stored in an embedded data field.
You could directly call the embedded data field in your question texts: ${e://Field/qsntxt}

Best regards

Rudi


You can't nest pipes, which it seems is what you are trying to do. You could add JS to QID40 to save the Question Text to an embedded data field.
var qsntxt = jQuery("#"+this.questionId+" .QuestionText").html();
Qualtrics.SurveyEngine.setEmbeddedData("qsntxt",qsntxt);
Then you can pipe

${e://Field/qsntxt}
into questions on subsequent pages.


thanks both I really appreciate it but the problem still remains. TomG your suggestion helps a lot thank you it does store the text of the question and does display it when I pipe that into another question! Rudi the reason I need to use javascript is because I don't know in advance which embedded var needs to be displayed until I calculate "top scores". Didn't mention it to keep it short but this is problem:
I have say 5 CVs each with embedded data "qsntxt1" "qsntxt2" ... that store the question content (thanks to Tom). Each CV is followed by two questions that ask the respondent to rate it from 1 - 10 on different parameters. The survey ends with displaying the top 3 CVs who have the highest "score" calculated by averaging their ratings from these two questions.
So the embedded fields on the final qsn could be qsntxt1, qsntxt3, qsntxt5 or any other combination depending on the respondents rating. One idea I tried is to identify the top3 qsntxt fields and resave them as embedded fields rank1 rank2 and rank3 so that I can just set the final question to pipe rank1 2 and 3 without knowing in advance what's in it. But this doesn't work because I guess that would be nesting as well?

Attached is the code to better explain what I'm doing in a 2 CVs :
Qualtrics.SurveyEngine.addOnload(function()
{
var ratings = [];
var cvs = {
cv1: ["qsntxt1","${q://QID1093/ChoiceGroup/SelectedChoices}","${q://QID44/ChoiceGroup/SelectedChoices}"], // this is : cv1 : [ embedded qsntxt for cv1 , collected rating 1-10 of cv1, another rating 1-10 of cv1 ]
  cv2: "qsntxt2","${q://QID1083/ChoiceGroup/SelectedChoices}","${q://QID1084/ChoiceGroup/SelectedChoices}"] // this is : cv2 : embedded qsntxt for cv2 , collected rating 1-10 of cv2, another rating 1-10 of cv2 ]
}

for (var i = 1; i < 3; i++) {
var cvname = 'cv'+ i; //getting cv1 or cv2 to gp through list cvs
var rate = parseInt(cvsscvname]]1]); //selected choice of first rating question 
var accept = parseInt(cvsvcvname]e2]); //selected choice of second rating question
var score = (rate + accept)/2; // average of the two = score
    ratings.push(ucvs[cvname]a0],score]); // add "qsntxt" and score to ratings list    
}


//sort ratings in descending order
ratings.sort(function(a, b){return be1]-a 1]});

//this doesnt work 
var soln1 = Qualtrics.SurveyEngine.getEmbeddedData(rank1); //qsntxt1 is a previously stored embedded datafield that contains the CV text 
Qualtrics.SurveyEngine.setEmbeddedData("soln1",soln1);
Infinite thanks!!



https://community.qualtrics.com/XMcommunity/discussion/comment/43360#Comment_43360You are pushing text and numbers into your rating array then trying to sort it, which isn't going to work. I have no idea what you are doing at the end; all that code does is get an embedded field and store it in a different embedded field. You never used the sorted ratings.
Instead of an object (cvs) that contains arrays, you should use an array that contains objects. You can store the scores in same array then sort it. It would be something like:
var cvs = [
{name:"cv1",
qtxt:"${e://Field/qsntxt1}",
rate1:parseInt("${q://QID1093/ChoiceGroup/SelectedChoices}"),
rate2:parseInt("${q://QID44/ChoiceGroup/SelectedChoices}"),
score:0
},
...etc...
];


Leave a Reply