How to create piped text in JavaScript and use its value? | XM Community
Solved

How to create piped text in JavaScript and use its value?

  • 25 October 2018
  • 10 replies
  • 493 views

Userlevel 5
Badge +6
  • Level 3 ●●●
  • 197 replies
Hello,
I want to create piped in javascript and then use the value which is stored in the corresponding element/field in my script. I have tried to concatenate the required string using "+", but it doesn't work. Would anyone know how to solve this?
Thanks for any ideas

This doesn't work:

var i = 1;
var x = "${q://QID4/SelectedAnswerRecode/"+i+"}";
alert(x); //the message box is empty

or

var myName = "someName";
var efield = "${e://Field/" +myName+"}";
alert(efield);

This works:

var x = "${q://QID4/SelectedAnswerRecode/1}";
alert(x);
icon

Best answer by TomG 26 October 2018, 00:07

View original

10 replies

Userlevel 7
Badge +33
For using value you have to store value in embedded variable and using it then like below

Qualtrics.SurveyEngine.setEmbeddedData('Rank3', Label3);
Userlevel 5
Badge +6
Thank you @bansalpeeyush29,
but I already have stored the value there in this way. Now I want to access it in JavaSrcipt which is connected to one of the next questions (e.g.: inside "Qualtrics.SurveyEngine.addOnload(function(){});") using piped text created from some strings and one variable.
Userlevel 7
Badge +33
There seems brackets issue, use something like below:-


`var i = 1;`
`var x = "${q://QID30/ChoiceTextEntryValue}/"+i+"";`
`alert(x); //the message box is empty`

Alert displayed like below, q30 for mine is text box

!
Userlevel 7
Badge +27
@fleb,

Piped values are resolved on the Qualtrics server before when the page is sent to the browser. So, you can't dynamically update piped values in JavaScript.
Userlevel 5
Badge +6
@bansalpeeyush29
Thanks a lot again, but I don't think there is the bracket issue you described. I don't want to print i after the value of the field. In my case, it is a part of the name of the filed. The first example is from a matrix table. Particular rows are numbered like this. The bracket issue also can't explain why the second example doesn't work...
Userlevel 5
Badge +6
@TomG
Thank you very much for your response. I forgot to mention that my questions are on different pages of the questionnaire. In that case, it should work, or am I wrong?
Userlevel 7
Badge +27
You're wrong (sorry). It doesn't matter what page they are on, your first two examples will NEVER work.
Userlevel 5
Badge +6
@TomG
OK, and could you explain to me why? I have found a definition of dynamic software updating on the internet and I don't think I'm doing it. And is there some other way how to access questions answers or embedded fields using the part of the string in the variable?
Userlevel 7
Badge +27
> @fleb said:
> @TomG
> OK, and could you explain to me why?
I already did in my original answer, but I'll elaborate. The Qualtrics server resolves the piped values when it sends the page to the server. JavaScript runs locally on your computer. So both `${q://QID4/SelectedAnswerRecode/"+i+"}` and `${e://Field/" +myName+"}` are invalid pipes as far the Qualtrics server is concerned and both will resolve as blank. So your JavaScript will actually be:
```
var i = 1;
var x = "";
alert(x);
```
or
```
var myName = "someName";
var efield = "";
alert(efield);
```
>And is there some other way how to access questions answers or embedded fields using the part of the string in the variable?
For embedded data you can use `Qualtrics.SurveyEngine.getEmbeddedData(name)` where you can dynamically construct the name.

For questions, you can pipe ALL the answers into an array:
```
answerArray = ["${q://QID4/SelectedAnswerRecode/1}",
"${q://QID4/SelectedAnswerRecode/2}",
"${q://QID4/SelectedAnswerRecode/3}",
etc...
];
```
Then dynamically access it by subtracting one from the index:
```
var i = 1;
var x = answerArray[i-1];
```
Userlevel 5
Badge +6
@TomG
Thank you very much, I did it like this in fact, but I thought there must be some more elegant way. Now I know there isn't and why.

Leave a Reply