Using piped text from loop and merge in Java script | XM Community
Solved

Using piped text from loop and merge in Java script

  • 16 March 2018
  • 5 replies
  • 68 views

Hi,

I have a problem with using loop and merge pipe text in java script. I try to create embedded data, which will be sum of two fields, and display it on the next page. I want also to create the code which will work on with loop and merge functionality. For summing the values, i used QIDs instead of piped text, as pipe text have some problems with displaying on the next page (it seems that it ned to be reloaded, by getting back to the question from which i am taking piped text).

here is some code i wrote:

_Qualtrics.SurveyEngine.addOnPageSubmit(function()
{
var nr = "${lm://Field/1}";
var name1 = "QR"+nr+"~QID2~1~1~TEXT";
var name2 = "QR"+nr+"~QID2~1~2~TEXT";
var sum = (name1^1 + name2^1)
{
Qualtrics.SurveyEngine.setEmbeddedData("sum", sum);
}
});_

However what i have tried, it seems that loop and merge from loop and merge: ${lm://Field/1}, does not work in java script.

I would great appreciate if anyone had similar problem or know how to use loop and merge variable in the java script.

Thanks!
Piotr
icon

Best answer by TomG 16 March 2018, 16:29

View original

5 replies

Userlevel 7
Badge +27
Your problem isn't specific to loop and merge. First, I don't think addOnPageSubmit is a function. I think you are looking for addOnUnload (which in my experience doesn't really work the way you think it would). Also, you are trying to do a bitwise XOR on the ids of two text fields, them sum them, which doesn't make any sense. You need to get the values of the text fields, then do something with them, but I doubt your really want to do a bitwise XOR.
Well, so i have tried the same code using it on the block without the loop and merge, and it seems to be working. Regarding adding the text values, i tried and creating a variable using the pattern var1 = "QID", and it returns the value of indicated question, not the text in parenthesis (QID).

So my main question is how i can use piped text ${lm://Field/1} in javascript?
Userlevel 7
Badge +27
Post the code that worked without the loop & merge.
Here you go:

Qualtrics.SurveyEngine.addOnPageSubmit(function()
{
var test1= $("QR~QID2~1~1~TEXT").value;
var test2= $("QR~QID2~1~2~TEXT").value;
var sum = (test1^1 + test2^1)
{
Qualtrics.SurveyEngine.setEmbeddedData("test", sum);
}
});

Then if i want to display test embedded variable value using piped text, on the next page it works fine. However in case of loop and merge this QID have an additional parameter indicating number of the loop, that's why i'm trying to use ${lm://Field/1}.

Just to clarify, in above code i changed * in my source code to ^, due to text editor recognized that as formatting italic.
Userlevel 7
Badge +27
Ok, that makes a lot more sense. Besides the ^, you were missing $, (, ), and .value in your first script. However, you really don't need the loop number. Try this:
```
var inputs = $(this.questionId).select('.InputText');
var name1 = inputs[0].value;
var name2 = inputs[1].value;
var sum = (name1*1 + name2*1);
Qualtrics.SurveyEngine.setEmbeddedData("sum", sum);
```
I left out the addOnPageSubmit, because for some reason the Community gives me a Access Denied error when I include it .

Leave a Reply