Add and Subtraction | XM Community
Skip to main content
Solved

Add and Subtraction

  • January 19, 2025
  • 4 replies
  • 49 views

Forum|alt.badge.img+3
  • Level 2 ●●
  • 17 replies

Hello everyone, I want to add two embedded data. One of the embedded data is a fixed number and the other is the addition of the income from the tasks. I can see the mathematical addition result in the console, but not on the qualtrics page. I did not understand why it’s happening. Could someone please help me with that?

Thank you in advance.

Best.

Best answer by TomG

You could do this without JavaScript. In survey flow:

Embedded Data:
   totalIncome = $e{ e://Field/guaranteedPayment + e://Field/totalECU }

You can pipe either ${e://Field/totalIncome} OR $e{ e://Field/guaranteedPayment + e://Field/totalECU } into a question (depending on if you do the survey flow calculation before or after the survey block)

If you want to calculate something in JavaScript and display it immediately in html, you need to update an element’s innerHTML. For example, if you had an html like this:

<div id="totalIncome"></div>

You could update it like this:

document.getElementById("totalIncome").innerHTML = totalIncome;

 

View original

4 replies

TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 5929 replies
  • January 20, 2025

@O.kisa,

Please post your JS code.


Forum|alt.badge.img+3
  • Author
  • Level 2 ●●
  • 17 replies
  • January 20, 2025
TomG wrote:

@O.kisa,

Please post your JS code.

Thank you. Here it is:

Qualtrics.SurveyEngine.addOnload(function() {

    var guaranteedPayment = parseInt("${e://Field/Guaranteed_Payment}");

    var totalECU = parseInt("${e://Field/Total_ECU}");

   

    console.log("Guaranteed_Payment:", guaranteedPayment);

    console.log("Total_ECU:", totalECU);

   

    if (isNaN(guaranteedPayment) || isNaN(totalECU)) {

        console.error("Error: Embedded data values are not valid numbers.");

    } else {

        var totalIncome = guaranteedPayment + totalECU;

       

        Qualtrics.SurveyEngine.setEmbeddedData("Total_Income", totalIncome);

        console.log("Total Income saved:", totalIncome);

    }

});


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 5929 replies
  • Answer
  • January 20, 2025

You could do this without JavaScript. In survey flow:

Embedded Data:
   totalIncome = $e{ e://Field/guaranteedPayment + e://Field/totalECU }

You can pipe either ${e://Field/totalIncome} OR $e{ e://Field/guaranteedPayment + e://Field/totalECU } into a question (depending on if you do the survey flow calculation before or after the survey block)

If you want to calculate something in JavaScript and display it immediately in html, you need to update an element’s innerHTML. For example, if you had an html like this:

<div id="totalIncome"></div>

You could update it like this:

document.getElementById("totalIncome").innerHTML = totalIncome;

 


Forum|alt.badge.img+3
  • Author
  • Level 2 ●●
  • 17 replies
  • January 21, 2025

 

TomG wrote:

You could do this without JavaScript. In survey flow:

Embedded Data:
   totalIncome = $e{ e://Field/guaranteedPayment + e://Field/totalECU }

You can pipe either ${e://Field/totalIncome} OR $e{ e://Field/guaranteedPayment + e://Field/totalECU } into a question (depending on if you do the survey flow calculation before or after the survey block)

If you want to calculate something in JavaScript and display it immediately in html, you need to update an element’s innerHTML. For example, if you had an html like this:

<div id="totalIncome"></div>

You could update it like this:

document.getElementById("totalIncome").innerHTML = totalIncome;

 

Thank you. Solved it.


Leave a Reply