Hi,
is there a way to check in a "constant sum" question, whether the total is "100 or less"?
The "must total" validation only checks whether the total is exactly 100. And I also haven't been able to identify any JS solution.
Thanks!
Manfred
Hi Manfred,
A colleague had a similar request, so the following JavaScript checked if the total was over 100. The key will be finding the ID of each box in your constant sum question. You can use your debugger to find those (replacing the QR~QID82#1 elements in this example).
The other thing this example is doing is displaying an error message, so after this question, on the same page, I have an empty question that just has this HTML in the question text: . The JavaScript is feeding the message (if needed) into that errorMsg.
Hope this helps!
Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/
const percentInputs = document.querySelectorAll("input[type=text].InputText");
//Every time someone types in a number add all the boxes together, update the running total, and display error if applicable.
function addItUp(){
if(document.getElementById('QR~QID82#1~1~1~TEXT').value > 100 ||
document.getElementById('QR~QID82#1~2~1~TEXT').value > 100 ||
document.getElementById('QR~QID82#1~3~1~TEXT').value > 100 ||
document.getElementById('QR~QID82#1~4~1~TEXT').value > 100){
document.getElementById("errorMsg").innerHTML = "You've entered a value greater than 100%.";
} else {
document.getElementById("errorMsg").innerHTML = " ";
}
}
//When page loads up we want to listen to all of the text boxes. Add the above function to the boxes.
for (index = 0; index < percentInputs.length; ++index) {
percentInputstindex].addEventListener("keyup", addItUp);
}
});
Thanks a lot, mvanepps!
I'll slightly adapt the script for my specific needs, but it's a very good starting point.
No problem!
Hi everyone, I know this question has been resolved, but I was wondering if someone could help e out with a modification to this code.
I am looking to have my constant slider sum be less than or equal to the value of a piped text from a previous question. I’ve adjusted the code but I am getting an error message, saying that there is an “unexpected value: {“
Code below:
Qualtrics.SurveyEngine.addOnReady(function()
{
const percentInputs = document.querySelectorAll("inputltype=text].InputText");
function addItUp(){
if(document.getElementById('QR~QID11#1~1~1~TEXT').value > ${q://QID40/ChoiceNumericEntryValue/1} ||
document.getElementById('QR~QID11#1~2~1~TEXT').value > ${q://QID40/ChoiceNumericEntryValue/1} ||
document.getElementById('QR~QID11#1~3~1~TEXT').value > ${q://QID40/ChoiceNumericEntryValue/1} ||
document.getElementById('QR~QID11#1~4~1~TEXT').value > ${q://QID40/ChoiceNumericEntryValue/1} ||
document.getElementById('QR~QID11#1~5~1~TEXT').value > ${q://QID40/ChoiceNumericEntryValue/1}){
document.getElementById("errorMsg").innerHTML = "You've entered a value greater than ${q://QID40/ChoiceNumericEntryValue/1}.";
} else {
document.getElementById("errorMsg").innerHTML = " ";
}
}
for (index = 0; index < percentInputs.length; ++index) {
percentInputsrindex].addEventListener("keyup", addItUp);
}
});
Any help would be much appreciated.
Thanks.
Leave a Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.