Solved
How to create piped text in JavaScript and use its value?
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);
Best answer by TomG
> @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];
```
View originalLeave a Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.