Get data for slider position instead of slider values | XM Community
Question

Get data for slider position instead of slider values

  • 14 July 2023
  • 1 reply
  • 129 views

Badge +1

I try to get data on the mere slider positions entered instead of the values which belonged to the positions entered with the slider.
That meants, I would like to know which distances the entered values have from each other in cm rather than knowing what numbers the participants entered with the slider.

 

I try to do this by using embedded data. I created an embedded data field in the survey flow just below the block with the slider question which is named “SliderPosition”.

In the slider question I added this JavaScript code so that I get shown in the embedded data afterwards the slider position:

Qualtrics.SurveyEngine.addOnload(function() {
    // Retrieve the slider element by its question ID
    var slider = this.getQuestionContainer().querySelector('input[type="range"]');

    // Get the minimum and maximum values of the slider
    var min = parseInt(slider.min);
    var max = parseInt(slider.max);

    // Add an event listener to capture the slider position
    slider.addEventListener('input', function() {
        // Get the position value
        var position = (parseInt(slider.value) - min) / (max - min);

        // Set the position value as embedded data
        Qualtrics.SurveyEngine.setEmbeddedData('SliderPosition', position);
    });
});

Qualtrics.SurveyEngine.addOnReady(function() {
    /* Place your JavaScript here to run when the page is ready */
});

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


However, in the Results section the embedded data field “SliderPosition” stays empty no matter how many results I gathered. Does somebody know what could be wrong here?


1 reply

Userlevel 3
Badge +10

Your selector isn’t working. Try using this Javascript instead:

 

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

// Retrieve the slider element by its question ID

// Get the minimum and maximum values of the slider
var track = document.getElementById(this.questionId + '~1~track');
var min = parseInt(track.getAttribute("aria-valuemin"));
var max = parseInt(track.getAttribute("aria-valuemax"));
var value = parseInt(jQuery("#" + this.questionId + " .ResultsInput:eq(0)").val());
// Get the position value
var position = (value - min) / (max - min);

// Set the position value as embedded data
Qualtrics.SurveyEngine.setEmbeddedData('SliderPosition', position);

});

 

(I also moved it to addOnPageSubmit and got rid of the event listener, since you only need to record the value when the user submits)

Leave a Reply