Slider Calculation | XM Community
Skip to main content
Question

Slider Calculation


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

Hello, in my study, if the sliders are marked as 50, the participants are paid 100 ECU. The income obtained from the sliders is called labor income. I used an embedded data and JS code to make this calculation. However, sometimes the slider income is calculated and sometimes it is not. What could be the reason for this?

12 replies

  • Level 4 ●●●●
  • 204 replies
  • February 6, 2025

Is your slider question and embedded data and the next question, are they in same block?

Are you using any randomizer?


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

It is as in the attached screenshot.


Forum|alt.badge.img+3
  • Author
  • Level 2 ●●
  • 17 replies
  • February 6, 2025
jbk wrote:

Is your slider question and embedded data and the next question, are they in same block?

Are you using any randomizer?

No, I am not using any randomizer.

 


  • Level 4 ●●●●
  • 204 replies
  • February 6, 2025

can you also show how the embedded data is being stored. and whats the purpose of JS?

 


Forum|alt.badge.img+3
  • Author
  • Level 2 ●●
  • 17 replies
  • February 6, 2025
jbk wrote:

can you also show how the embedded data is being stored

 

Thank you. Of course. Here it is;

 


  • Level 4 ●●●●
  • 204 replies
  • February 6, 2025
O.kisa wrote:
jbk wrote:

can you also show how the embedded data is being stored

 

Thank you. Of course. Here it is;

 

so, the purpose of JS is to store the values for Tota_ECU. IS that right?


Forum|alt.badge.img+3
  • Author
  • Level 2 ●●
  • 17 replies
  • February 6, 2025
jbk wrote:
O.kisa wrote:
jbk wrote:

can you also show how the embedded data is being stored

 

Thank you. Of course. Here it is;

 

so, the purpose of JS is to store the values for Tota_ECU. IS that right?

Yes, you are right.
Here is my JS code:
 

Qualtrics.SurveyEngine.addOnload(function() {
    var that = this;
    var timer = 20; // 20 seconds timer
    var interval = setInterval(function() {
        timer--;
        if (timer <= 0) {
            clearInterval(interval);
            that.clickNextButton();
        }
    }, 1000);

    // Disable sliders after 20 seconds
    setTimeout(function() {
        jQuery('.q-slider input[type="range"]').prop('disabled', true);
    }, 20000);
});

Qualtrics.SurveyEngine.addOnReady(function() {
    var that = this;
    var earnings = 0;

    // Calculate earnings based on sliders marked as 50%
    jQuery('.q-slider input[type="range"]').on('change', function() {
        var value = jQuery(this).val();
        if (value == 50) {
            earnings += 100;
        }
        // Update embedded data each time a slider is changed
        that.setEmbeddedData('earnings', earnings);
    });
});
Qualtrics.SurveyEngine.addOnReady(function() {
    var totalPercentage = 0;
    for (var i = 1; i <= 10; i++) {
        var sliderValue = parseInt("${q://QID" + i + "/ChoiceTextEntryValue}");
        if (!isNaN(sliderValue)) {
            totalPercentage += sliderValue;
        }
    }
    totalPercentage = totalPercentage / 10;
    Qualtrics.SurveyEngine.setEmbeddedData("totalPercentage", totalPercentage);
});
Qualtrics.SurveyEngine.addOnReady(function() {
    var totalPercentage = 0;
    for (var i = 1; i <= 10; i++) {
        var sliderValue = parseInt("${q://QID" + i + "/ChoiceTextEntryValue}");
        console.log("Slider " + i + " value: " + sliderValue);
        if (!isNaN(sliderValue)) {
            totalPercentage += sliderValue;
        }
    }
    totalPercentage = totalPercentage / 10;
    console.log("Total Percentage: " + totalPercentage);
    Qualtrics.SurveyEngine.setEmbeddedData("totalPercentage", totalPercentage);
});
Qualtrics.SurveyEngine.addOnload(function() {
    var totalECU = 0;
    var that = this;
    this.questionclick = function(event, element) {
        totalECU = 0;
        for (var i = 1; i <= 10; i++) {
            var sliderValue = that.getChoiceValue(i);
            if (sliderValue == 50) {
                totalECU += 100;
            }
        }
        Qualtrics.SurveyEngine.setEmbeddedData("Total_ECU", totalECU);
    };
});


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 5923 replies
  • February 6, 2025

@O.kisa,

Here is one error:

var sliderValue = parseInt("${q://QID" + i + "/ChoiceTextEntryValue}");

will always result in NaN. You can’t dynamically build piped strings. Pipes are resolved on the server before the page is sent to the browser. So you are always parsing a non-numeric string.


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

@O.kisa,

Here is one error:

var sliderValue = parseInt("${q://QID" + i + "/ChoiceTextEntryValue}");

will always result in NaN. You can’t dynamically build piped strings. Pipes are resolved on the server before the page is sent to the browser. So you are always parsing a non-numeric string.

Okay thank you so much. What do ı need to use instead of it? or should ı delete it?


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 5923 replies
  • February 6, 2025
O.kisa wrote:
TomG wrote:

@O.kisa,

Here is one error:

var sliderValue = parseInt("${q://QID" + i + "/ChoiceTextEntryValue}");

will always result in NaN. You can’t dynamically build piped strings. Pipes are resolved on the server before the page is sent to the browser. So you are always parsing a non-numeric string.

Okay thank you so much. What do ı need to use instead of it? or should ı delete it?

I don’t know what totalPercentage is used for. The way you are attempting to calculate it doesn’t make sense (you are trying to get values from 10 different questions with QIDs QID1 through QID10).  


Forum|alt.badge.img+3
  • Author
  • Level 2 ●●
  • 17 replies
  • February 6, 2025
TomG wrote:
O.kisa wrote:
TomG wrote:

@O.kisa,

Here is one error:

var sliderValue = parseInt("${q://QID" + i + "/ChoiceTextEntryValue}");

will always result in NaN. You can’t dynamically build piped strings. Pipes are resolved on the server before the page is sent to the browser. So you are always parsing a non-numeric string.

Okay thank you so much. What do ı need to use instead of it? or should ı delete it?

I don’t know what totalPercentage is used for. The way you are attempting to calculate it doesn’t make sense (you are trying to get values from 10 different questions with QIDs QID1 through QID10).  

Yes, there are 10 sliders in the question. Totalpercentage allows adding 100 ECU to the sliders marked as 50%.


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 5923 replies
  • February 6, 2025
O.kisa wrote:

Yes, there are 10 sliders in the question. Totalpercentage allows adding 100 ECU to the sliders marked as 50%.

For the current question, get the slider input values (like you did in your change function) or use the API’s getChoiceValue() function.


Leave a Reply