Add and Subtraction | XM Community
Skip to main content

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.

@O.kisa,

Please post your JS code.


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

    }

});


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;

 


 

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