Piping text using embedded data created in the javascript api | XM Community
Question

Piping text using embedded data created in the javascript api


Userlevel 2
Badge +4

Hi

 

I have a question in my survey which needs to pull through a previous (numeric) response from another question. I perform some functions on the response then pipe the one of the values depending on another constraint. Some of the values may seem irrelevant. For the ones I care about I have tried creating embedded variables in the api but they either aren’t working or the output isn’t being displayed through the piped text. I will attach the code from the various questions.

 

Here is the piped text I’m using in the next question block:

${e://Field/Score} - ${e://Field/T5} - 10

${e://Field/Score} - ${e://Field/t5}

 

The other issue is that this displays the sum of their final score but it would be better displayed as one value so the calculation goes on behind the scenes.

 

Many thanks in advance

Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/

});

Qualtrics.SurveyEngine.addOnReady(function()
{

/*Place your JavaScript here to run when the page is fully displayed*/

});

Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/

Qualtrics.SurveyEngine.setEmbeddedData("reported_income", y);

function Calc() {

let text=document.querySelector("input[type=text]")

let y1;


let y=parseInt(text.value);
let score=parseInt(' ${gr://SC_cT0uGBHILsp0UTQ/Score} ');

if (y === 0) {
y1 = 0;
} else if (y > 10) {
y1 = 10;
} else {
y1 = y;
}


let y2;
if (y <= 10) {
y2 = 0;
} else if (y > 20) {
y2 = 10;
} else {
y2 = (y - 10);
}



let y3;
if (y <= 20) {
y3 = 0;
} else if (y > 30) {
y3 = 10;
} else {
y3 = (y - 20);
}



let y4;
if (y <= 30) {
y4 = 0;
} else {
y4 = (y - 30);
}



let y5=y;



let c1;
if (score === 0) {
c1 = 0;
} else if (score > 10) {
c1 = 10;
} else {
c1 = score;
}

let c2;
if (score <= 10) {
c2 = 0;
} else if (score > 20) {
c2 = 10;
} else {
c2 = (score - 10);
}

let c3;
if (score <= 20) {
c3 = 0;
} else if (score > 30) {
c3 = 10;
} else {
c3 = (score - 20);
}


let c4;
if (score <= 30) {
c4 = 0;
} else {
c4 = (score - 30);
}


let c5=score;

let t1=0


let t2=(y2*0.2)


let t3=(y3*0.3)


let t4=(y4*0.4)

Qualtrics.SurveyEngine.setEmbeddedData("taxdue - reported", t5);
let t5=(t2+t3+t4)





let T1=0


let T2=(c2*0.2)


let T3=(c3*0.3)


let T4=(c4*0.4)

Qualtrics.SurveyEngine.setEmbeddedData("taxdue - reported", T5);
let T5=(T2+T3+T4)





let yt1=10


let yt2=(y2 - t2)


let yt3=(y3 - t3)


let yt4=(y4 - t4)


let yt5=(yt1+yt2+yt3+yt4)



let cT1=10


let cT2=(c2 - T2)


let cT3=(c3 - T3)


let cT4=(c4 - T4)


let cT5=(cT1+cT2+cT3+cT4)





let e1=(0)


let e2=(score)
if (y === score) {
e2 = 0;
} else if (y < score) {
e2 = (c2 - y2);
}


let e3=(score)
if (y === score) {
e3 = 0;
} else if (y < score) {
e3 = (c3 - y3);
}


let e4=(score)
if (y === score) {
e4 = 0;
} else if (y < score) {
e4 = (c4 - y4);
}


Qualtrics.SurveyEngine.setEmbeddedData("evaded", e5);

let e5=(e2 + e3 + e4)


Qualtrics.SurveyEngine.setEmbeddedData("total", total);

let total=(yt5 + score - y)




}

Calc();

});

 


3 replies

Userlevel 7
Badge +27

Three things to start:

  1. You should be using addOnPageSubmit() instead of addOnUnload()
  2. In your first line of code y is undefined.
  3. The fields you are piping (score, T5, t5) aren’t embedded data fields, they are JavaScript variables
Userlevel 2
Badge +4

Thank you for this. I have amended the first two as suggested. Do you know how I may pipe text into another question using the set embedded data and get embedded data functions that are in the code (if possible)?

Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/

});

Qualtrics.SurveyEngine.addOnReady(function()
{

/*Place your JavaScript here to run when the page is fully displayed*/

});

Qualtrics.SurveyEngine.addOnPageSubmit(function()
{
/*Place your JavaScript here to run when the page is unloaded*/





function Calc() {

let text=document.querySelector("input[type=text]")

let y1;


let y=parseInt(text.value);
let score=parseInt(' ${gr://SC_cT0uGBHILsp0UTQ/Score} ');

Qualtrics.SurveyEngine.setEmbeddedData("reported_income", y);

if (y === 0) {
y1 = 0;
} else if (y > 10) {
y1 = 10;
} else {
y1 = y;
}


let y2;
if (y <= 10) {
y2 = 0;
} else if (y > 20) {
y2 = 10;
} else {
y2 = (y - 10);
}



let y3;
if (y <= 20) {
y3 = 0;
} else if (y > 30) {
y3 = 10;
} else {
y3 = (y - 20);
}



let y4;
if (y <= 30) {
y4 = 0;
} else {
y4 = (y - 30);
}



let y5=y;



let c1;
if (score === 0) {
c1 = 0;
} else if (score > 10) {
c1 = 10;
} else {
c1 = score;
}

let c2;
if (score <= 10) {
c2 = 0;
} else if (score > 20) {
c2 = 10;
} else {
c2 = (score - 10);
}

let c3;
if (score <= 20) {
c3 = 0;
} else if (score > 30) {
c3 = 10;
} else {
c3 = (score - 20);
}


let c4;
if (score <= 30) {
c4 = 0;
} else {
c4 = (score - 30);
}


let c5=score;

let t1=0


let t2=(y2*0.2)


let t3=(y3*0.3)


let t4=(y4*0.4)

Qualtrics.SurveyEngine.setEmbeddedData("taxdue - reported", t5);
let t5=(t2+t3+t4)





let T1=0


let T2=(c2*0.2)


let T3=(c3*0.3)


let T4=(c4*0.4)

Qualtrics.SurveyEngine.setEmbeddedData("taxdue - reported", T5);
let T5=(T2+T3+T4)





let yt1=10


let yt2=(y2 - t2)


let yt3=(y3 - t3)


let yt4=(y4 - t4)


let yt5=(yt1+yt2+yt3+yt4)



let cT1=10


let cT2=(c2 - T2)


let cT3=(c3 - T3)


let cT4=(c4 - T4)


let cT5=(cT1+cT2+cT3+cT4)





let e1=(0)


let e2=(score)
if (y === score) {
e2 = 0;
} else if (y < score) {
e2 = (c2 - y2);
}


let e3=(score)
if (y === score) {
e3 = 0;
} else if (y < score) {
e3 = (c3 - y3);
}


let e4=(score)
if (y === score) {
e4 = 0;
} else if (y < score) {
e4 = (c4 - y4);
}


Qualtrics.SurveyEngine.setEmbeddedData("evaded", e5);

let e5=(e2 + e3 + e4)


Qualtrics.SurveyEngine.setEmbeddedData("total", total);

let total=(yt5 + score - y)




}

Calc();

});

 

Userlevel 7
Badge +27

Thank you for this. I have amended the first two as suggested. Do you know how I may pipe text into another question using the set embedded data and get embedded data functions that are in the code (if possible)?

Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/

});

Qualtrics.SurveyEngine.addOnReady(function()
{

/*Place your JavaScript here to run when the page is fully displayed*/

});

Qualtrics.SurveyEngine.addOnPageSubmit(function()
{
/*Place your JavaScript here to run when the page is unloaded*/





function Calc() {

let text=document.querySelector("input[type=text]")

let y1;


let y=parseInt(text.value);
let score=parseInt(' ${gr://SC_cT0uGBHILsp0UTQ/Score} ');

Qualtrics.SurveyEngine.setEmbeddedData("reported_income", y);

if (y === 0) {
y1 = 0;
} else if (y > 10) {
y1 = 10;
} else {
y1 = y;
}


let y2;
if (y <= 10) {
y2 = 0;
} else if (y > 20) {
y2 = 10;
} else {
y2 = (y - 10);
}



let y3;
if (y <= 20) {
y3 = 0;
} else if (y > 30) {
y3 = 10;
} else {
y3 = (y - 20);
}



let y4;
if (y <= 30) {
y4 = 0;
} else {
y4 = (y - 30);
}



let y5=y;



let c1;
if (score === 0) {
c1 = 0;
} else if (score > 10) {
c1 = 10;
} else {
c1 = score;
}

let c2;
if (score <= 10) {
c2 = 0;
} else if (score > 20) {
c2 = 10;
} else {
c2 = (score - 10);
}

let c3;
if (score <= 20) {
c3 = 0;
} else if (score > 30) {
c3 = 10;
} else {
c3 = (score - 20);
}


let c4;
if (score <= 30) {
c4 = 0;
} else {
c4 = (score - 30);
}


let c5=score;

let t1=0


let t2=(y2*0.2)


let t3=(y3*0.3)


let t4=(y4*0.4)

Qualtrics.SurveyEngine.setEmbeddedData("taxdue - reported", t5);
let t5=(t2+t3+t4)





let T1=0


let T2=(c2*0.2)


let T3=(c3*0.3)


let T4=(c4*0.4)

Qualtrics.SurveyEngine.setEmbeddedData("taxdue - reported", T5);
let T5=(T2+T3+T4)





let yt1=10


let yt2=(y2 - t2)


let yt3=(y3 - t3)


let yt4=(y4 - t4)


let yt5=(yt1+yt2+yt3+yt4)



let cT1=10


let cT2=(c2 - T2)


let cT3=(c3 - T3)


let cT4=(c4 - T4)


let cT5=(cT1+cT2+cT3+cT4)





let e1=(0)


let e2=(score)
if (y === score) {
e2 = 0;
} else if (y < score) {
e2 = (c2 - y2);
}


let e3=(score)
if (y === score) {
e3 = 0;
} else if (y < score) {
e3 = (c3 - y3);
}


let e4=(score)
if (y === score) {
e4 = 0;
} else if (y < score) {
e4 = (c4 - y4);
}


Qualtrics.SurveyEngine.setEmbeddedData("evaded", e5);

let e5=(e2 + e3 + e4)


Qualtrics.SurveyEngine.setEmbeddedData("total", total);

let total=(yt5 + score - y)




}

Calc();

});

 

You are saving t5 and T5 into the embedded data field ‘taxdue - reported’  so you would pipe that field (maybe they should be two different fields?). I recommend modifying it to ‘taxdue_reported’ so it doesn’t include spaces or a dash.  Then you can pipe it as ${e://Field/taxdue_reported}

Leave a Reply