Validate Constant Sum Total as Less Than or Equal to Previous Survey Question | XM Community
Skip to main content

Hello Qualtrics Community, 

 

I am attempting to validate the total of a constant sum question as being less than or equal to the numeric response in a previous question in order to advance in a survey. It does not appear there are any built in Qualtrics solutions to achieve this, so I have attempted this using javascript based, which is based on a solution offered in a different question and answer, found here: Constant Sum Total Must Be Less than or Equal to Embedded Data Value | XM Community (qualtrics.com) provided by @GrayMatterThinking. I have a question out to GrayMatterThinking on that page, but I do not know if they are still active. 

 

It appears the script is designed to implement what I am looking for (i.e., a less than or equal to requirement), but it calls embedded data rather than data from a previously answered question, so I believe I only needed to edit the script to call the appropriate variable data. I did this using Inspect to identify the correct question reference IDs for both the prior question value and the total value in the constant sum question. The script saved without error, but it is not working. Specifically, I am unable to advance beyond the page that contains the javascript, regardless of the values I enter. Additionally, the script altered the next button to an unacceptable format (I may be able to figure this out on my own; functionality is most important at this point). 

 

Any help will be greatly appreciated. 

 

Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
var warnings = ];

function hideEl(element) {
if($(element)) $(element).hide();
}

function createNextButton() {
var nextButtonHTML = '<input id="CustomNextButton" class="NextButton Button" title="→" type="button" name="NextButton" value="→" aria-label="Next">';
jQuery('#Buttons').append(nextButtonHTML);
}

hideEl.defer('NextButton');
createNextButton();

});

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

jQuery('#CustomNextButton').on('click', function() {

/* This is where you reference the Embedded Data field */
var xED_Field = Number(document.getElementById('QR~QID4').value);
/* This is where you sum the values entered on the current page - Inspect the HTML to find the correct Element ID */
var xtotal =Number(document.getElementById('QID12~1_Total').value);

/* This is where you set your condition to be accepted */
if (xtotal <= xED_Field) {
jQuery('#NextButton').click();
jQuery('#CustomNextButton').hide();
} else {
/* This is where you set your error message text */
var errorMsg = "Responses must sum to " + xED_Field + ".";
var x = document.createElement("DIV");
var t = document.createTextNode(errorMsg);
x.className = "custom-warning";
x.appendChild(t);
document.getElementById('Questions').parentElement.appendChild(x);
jQuery('.custom-warning').css("background", "pink");
jQuery('.custom-warning').css("color", "red");
jQuery('.custom-warning').css("font-size", "12px");

}
});

});

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

@JMSurveys I would recommend using custom validation and keeping both the questions in seperate pages. Within custom validation you can pipe the previous questions response as the value.

If you would like to keep it on same page only then JS is required.

Hope it helps!


@JMSurveys I would recommend using custom validation and keeping both the questions in seperate pages. Within custom validation you can pipe the previous questions response as the value.

If you would like to keep it on same page only then JS is required.

Hope it helps!

Thank you for your response. Are you familiar with JS required to do this? 


Leave a Reply