Pipe largest value of a multiple choice question which allows more than one response | XM Community
Solved

Pipe largest value of a multiple choice question which allows more than one response

  • 19 April 2024
  • 2 replies
  • 23 views

Badge +1

I have a multiple choice question that is simply this:

Which of the following years did you participate (select all that apply)?
2019
2020
2021
2022
2023

A later question asks:

Relative to ${e://Field/largestYear}, how would you rate the following?

The embedded data value I want as “largestYear” is the highest selected year (most recent year) in the prior question.

This is the jscript attached to the first question:

Qualtrics.SurveyEngine.addOnload(function() {
// Wait until the respondent has interacted with the question
this.questionclick = function(event, element) {
// Initialize max value
var max = 2019;
// Loop through selected choices
jQuery("#"+this.questionId+" .ChoiceStructure tbody tr").each(function() {
// Check if choice is selected
if (jQuery(this).find(".Selected").length > 0) {
// Retrieve numerical value of choice
var value = parseFloat(jQuery(this).find("td:first").text());
// Update max value if necessary
if (value > max) {
max = value;
}
}
});
// Set the embedded data with the largest value
Qualtrics.SurveyEngine.setEmbeddedData('largestYear', max);
}
});

The resulting imbedded data in question two always turns out to be equal to the assignment in the jscript line var max = 2019 no matter what (numeric) value I put into that statement. This can’t be that hard. I’ve tried another approach like creating a function to call, but this produces no returned value:

Qualtrics.SurveyEngine.addOnload(function() {
// Function to find the largest selected label
function findLargestLabel() {
var largestLabel = ""; // Initialize largest label variable
var largestValue = -Infinity; // Initialize largest value variable

// Loop through each choice
this.questionChoices.forEach(function(choice) {
// Check if choice is selected and its value is greater than current largest value
if (choice.selected && parseFloat(choice.value) > largestValue) {
largestValue = parseFloat(choice.value);
largestLabel = choice.display;
}
});

return largestLabel; // Return the largest label
}

// Set embedded data with the largest label when question is clicked
this.questionclick = function(event, element) {
var largestLabel = findLargestLabel(); // Call the function to find the largest label
Qualtrics.SurveyEngine.setEmbeddedData('largestYear', largestLabel); // Set embedded data
};
});

I can’t for the life of me figure out how to solve this seemingly simple problem, how to pipe the largest value of a multiple choice selection to the next question. Any assistance will be greatly appreciated. This question is also posted on Stack Overflow. Sorry for the cross posting. 

icon

Best answer by vgayraud 19 April 2024, 08:24

View original

2 replies

Userlevel 6
Badge +38

Hi,

this.questionclick = function(event, element) {
Qualtrics.SurveyEngine.setEmbeddedData('selectedChoice', this.getQuestionInfo().Choices[this.getSelectedChoices().last()].Text);
}

 

Badge +1

Vincent - worked perfectly! Thanks.

Leave a Reply