skip to Main Content

I have made the following code Javascript code to count the number of times 0, 5, or 10 are selected in questions throughout my survey on Qualtrics. I place this code in each question:


Qualtrics.SurveyEngine.addOnload(function() {
    var isCorrect10 = 10;
    var currentCounter10 = Qualtrics.SurveyEngine.getEmbeddedData("Counter10");
    
    if (!currentCounter10) {
        currentCounter10 = 0;
    }
    
    var isCorrect0 = 0;
    var currentCounter0 = Qualtrics.SurveyEngine.getEmbeddedData("Counter0");
    
    if (!currentCounter0) {
        currentCounter0 = 0;
    }
    
    var isCorrect5 = 5;
    var currentCounter5 = Qualtrics.SurveyEngine.getEmbeddedData("Counter5");
    
    if (!currentCounter5) {
        currentCounter5 = 5;
    }
    

    // If it's selected, increment 
    if (isCorrect10) {
        currentCounter10++;
        Qualtrics.SurveyEngine.setEmbeddedData("Counter10",currentCounter10.toString());
    }
    
    if (isCorrect0) {
        currentCounter0++;
        Qualtrics.SurveyEngine.setEmbeddedData("Counter0",currentCounter0.toString());
    }
    
    if (isCorrect5) {
        currentCounter5++;
        Qualtrics.SurveyEngine.setEmbeddedData("Counter5",currentCounter5.toString());
    }
    

});



For this I initialize embedded data for Counter0, Counter5 and Counter10, all at 0.

This works perfectly for questions where it is just one singular slider. However, I want this to work for matrix questions were there are multiple questions. I want to count every appearance of count in each slider of a matrix, and also be compatible with the non-matrix questions. So each count (0,5,10) should be the total of all selections across all questions. How can I modify the javascript accordingly?

EDIT:

I had thought that the above code works, but it appears to not even work sufficiently for the single slider questions. Instead it just merely logs 1 value in the counter for each single slider, regardless of the presence of the values in question.

So, how can I make a counter that logs the presence of each value in question in all sliders throughout my survey?

EDIT 2

Thanks to the comment below, I’ve modified the code to I think be closer now but it still doesn’t fully work. Currently, I have the following:


Qualtrics.SurveyEngine.addOnload(function() {
    // Get current question ID
    var currentQuestionID = this.getQuestionInfo().QuestionID;
    
    // Define result names for counters
    var Counter10 = "Counter10";
    var Counter0 = "Counter0";
    var Counter5 = "Counter5";
    
    // Retrieve and parse embedded data values for counters
    var currentCounter10 = parseInt(Qualtrics.SurveyEngine.getEmbeddedData(Counter10), 10) || 0;
    var currentCounter0 = parseInt(Qualtrics.SurveyEngine.getEmbeddedData(Counter0), 10) || 0;
    var currentCounter5 = parseInt(Qualtrics.SurveyEngine.getEmbeddedData(Counter5), 10) || 0;

    // Function to get the selected value from a slider based on its number
    function getSliderValue(sliderNumber) {
        var sliderId = currentQuestionID + '_' + sliderNumber;
        var responseTextField = document.getElementById(sliderId);
        if (responseTextField) {
            return parseInt(responseTextField.value, 10); // Convert to integer
        } else {
            console.log("Element with ID " + sliderId + " not found.");
            return null;
        }
    }

    document.getElementById('NextButton').onclick = function(event) {
        // Assumingup to 5 sliders, since 4 is max
        for (var i = 1; i <= 5; i++) {
            var selectedValue = getSliderValue(i);
            if (selectedValue !== null) {
                // Increment the appropriate counter based on the selected value
                if (selectedValue === 10) {
                    currentCounter10++;
                    Qualtrics.SurveyEngine.setEmbeddedData(Counter10, currentCounter10.toString());
                } else if (selectedValue === 0) {
                    currentCounter0++;
                    Qualtrics.SurveyEngine.setEmbeddedData(Counter0, currentCounter0.toString());
                } else if (selectedValue === 5) {
                    currentCounter5++;
                    Qualtrics.SurveyEngine.setEmbeddedData(Counter5, currentCounter5.toString());
                }
                // Exit loop after processing the selected value
                break;
            }
        }

        // Proceed to the next question
        Qualtrics.SurveyEngine.navClick(event, 'NextButton');
    };
});

This still results in all 0s for all my counters. I think this is because it doesn’t work to select and evaluate the values of each individual slider with a group of sliders. This was my best attempt, but can anyone show an example of how to evaluate and increment based on every level of the same multi-slider question?

EDIT 3

I’m a bit closer now, thanks to great help in the comments, but I still can’t get it to work fully. I can now read the embedded data values set at 0 and get/store the selected answers as a variable, but I can’t seem to increment based on the selected values. My code is the following:

Qualtrics.SurveyEngine.addOnPageSubmit(function (type) { // To record the selection when the page is submitted

if (type == "next") {

var selChoice = this.getSelectedChoices();

// Set Embedded Data
    
var currentcount0 = parseInt(Qualtrics.SurveyEngine.getEmbeddedData('count0'),10); 
var currentcount5 = parseInt(Qualtrics.SurveyEngine.getEmbeddedData('count5'),10);
var currentcount10 = parseInt(Qualtrics.SurveyEngine.getEmbeddedData('count10'),10);

if (selChoice == 5) {

const newcount5 = currentcount5 + 1 ;
Qualtrics.SurveyEngine.setEmbeddedData("count5", newcount5)};

if (selChoice == 0) {

const newcount0 = currentcount0 + 1 ;
Qualtrics.SurveyEngine.setEmbeddedData("count0", newcount0)};
    
if (selChoice == 10) {

const newcount10 = currentcount10 + 1 ;
Qualtrics.SurveyEngine.setEmbeddedData("count10", newcount10)};
    
    
}
    
    
    
});

When this runs, the values of the count variables stay at 0, regardless of what is chosen. Might this be because the data is not read as numeric? Is there some other issue with simply adding +1 to the values?

2

Answers


  1. Chosen as BEST ANSWER

    Thanks to @Hargne's tremendous help, I was able to find a solution:

    Qualtrics.SurveyEngine.addOnPageSubmit(function (type) { // To record the selection when the page is submitted
    
    if (type == "next") {
    // Extract selected Answers for each slider level individually
    var selChoice1 = this.getChoiceAnswerValue(1)
    var selChoice2 = this.getChoiceAnswerValue(2)
    var selChoice3 = this.getChoiceAnswerValue(3)
    var selChoice4 = this.getChoiceAnswerValue(4)
    
    // Set Embedded Data
        
    var currentcount0 = parseInt(Qualtrics.SurveyEngine.getEmbeddedData('count0'),10); 
    var currentcount5 = parseInt(Qualtrics.SurveyEngine.getEmbeddedData('count5'),10);
    var currentcount10 = parseInt(Qualtrics.SurveyEngine.getEmbeddedData('count10'),10);
    
    // Increment each counter for each slider if values is selected
        
    if (selChoice1 == 5) {
    var newcount5 = currentcount5 += 1 ;
    Qualtrics.SurveyEngine.setEmbeddedData("count5", newcount5)};
    if (selChoice1 == 0) {
    var newcount0 = currentcount0 += 1 ;
    Qualtrics.SurveyEngine.setEmbeddedData("count0", newcount0)};
    if (selChoice1 == 10) {
    var newcount10 = currentcount10 += 1 ;
    Qualtrics.SurveyEngine.setEmbeddedData("count10", newcount10)};
        
        
    if (selChoice2 == 5) {
    var newcount5 = currentcount5 += 1 ;
    Qualtrics.SurveyEngine.setEmbeddedData("count5", newcount5)};
    if (selChoice2 == 0) {
    var newcount0 = currentcount0 += 1 ;
    Qualtrics.SurveyEngine.setEmbeddedData("count0", newcount0)};
    if (selChoice2 == 10) {
    var newcount10 = currentcount10 += 1 ;
    Qualtrics.SurveyEngine.setEmbeddedData("count10", newcount10)}; 
        
        
    if (selChoice3 == 5) {
    var newcount5 = currentcount5 += 1 ;
    Qualtrics.SurveyEngine.setEmbeddedData("count5", newcount5)};
    if (selChoice3 == 0) {
    var newcount0 = currentcount0 += 1 ;
    Qualtrics.SurveyEngine.setEmbeddedData("count0", newcount0)};
    if (selChoice3 == 10) {
    var newcount10 = currentcount10 += 1 ;
    Qualtrics.SurveyEngine.setEmbeddedData("count10", newcount10)}; 
        
    if (selChoice4 == 5) {
    var newcount5 = currentcount5 += 1 ;
    Qualtrics.SurveyEngine.setEmbeddedData("count5", newcount5)};
    if (selChoice4 == 0) {
    var newcount0 = currentcount0 += 1 ;
    Qualtrics.SurveyEngine.setEmbeddedData("count0", newcount0)};
    if (selChoice4 == 10) {
    var newcount10 = currentcount10 += 1 ;
    Qualtrics.SurveyEngine.setEmbeddedData("count10", newcount10)}; 
            
    }
        
    });
    

    The main issue is that the getSelectedAnswers() function doesn't seem to work with the slider, so instead the values must be extracted manually for each individual slider within the question. The above does the latter, so just make sure to initialize the count0, count5 and count10 embedded data within the survey flow and this will run.


  2. I noticed a couple of issues with the code above:

    • First of all, the if (!currentCounter5) { statement can cause unintentional issues.
      In Javascript, all of the following will be evaluated as falsy: null, undefined AND 0. If currentCounter5 0, then it would be treated as undefined (which I assume is incorrect behaviour).
      The solution to this would be to use if (isNaN(currentCounter5)) {

    • Secondly, the statement if (isCorrect10) { will always be true since isCorrect10 is set to 10 and then not changed. Likewise, isCorrect0 is set to 0, which means that this code block never will run (remember, because 0 is treated as falsy in Javascript). I would change these to something in the lines of if (isCorrect0 !== 0) { – however, as you aren’t reassigning these variables, I cannot fully grasp what these if-blocks are meant to do.

    I would also recommend to parse the response from the embedded data to make sure that you get integers (and not anything else, like strings).
    You can do this by wrapping the calls in parseInt:

    var currentCounter10 = parseInt(Qualtrics.SurveyEngine.getEmbeddedData("Counter10"), 10);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search