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

flebfleb Czech RepublicCommunity Member Sage ✭✭✭

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

  • TomGTomG Raleigh, NC Wizard ✭✭✭✭✭
    Accepted Answer

    @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];
    

Answers

  • bansalpeeyush29bansalpeeyush29 IndiaCommunity Member Wizard ✭✭✭✭✭

    For using value you have to store value in embedded variable and using it then like below

    Qualtrics.SurveyEngine.setEmbeddedData('Rank3', Label3);

  • flebfleb Czech RepublicCommunity Member Sage ✭✭✭

    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.

  • bansalpeeyush29bansalpeeyush29 IndiaCommunity Member Wizard ✭✭✭✭✭
    edited October 2018

    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

  • TomGTomG Raleigh, NCCommunity Member, Product Ideas Wizard ✭✭✭✭✭

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

  • flebfleb Czech RepublicCommunity Member Sage ✭✭✭

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

  • flebfleb Czech RepublicCommunity Member Sage ✭✭✭

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

  • TomGTomG Raleigh, NCCommunity Member, Product Ideas Wizard ✭✭✭✭✭

    You're wrong (sorry). It doesn't matter what page they are on, your first two examples will NEVER work.

  • flebfleb Czech RepublicCommunity Member Sage ✭✭✭

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

  • flebfleb Czech RepublicCommunity Member Sage ✭✭✭

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

Sign In to Comment