Force response with JavaScript and HTML questions | XM Community
Skip to main content

I'm working on a questionnaire which has a couple of custom questions based on JS and HTML. I would like to make sure that the responders answer all of the questions, but am having trouble implementing a forced response on Qualtrics.
My first approach was to use embedded variables and answer validation. I managed to create an embedded variable and update it such that it indicates whether the question has been answered (0 if not answered and 100 if answered). I then tried to compare it to the value of a random question (ranging between 1-4) using answer validation to show an error when the answer is greater than the embedded variable. The problem with this approach is that it turns out that the variable will only be updated with the progression to the next page and therefore cant be used to validate the answers on the current page.
I thought of trying to feed the embedded variable into a text field question and use answer validation on that question, but haven't managed in doing so.
Any help or ideas would be greatly appreciated 🙂

Managed to solve it.
I ended up adding a hidden "Constant Sum" question and using JS to temporarily disable the next button and alter it's text .
Each field in the constant sum question would get updated from 0 to 1 when a question was answered, using the following JS code in each question:
   var inputNumber = document.getElementById('QR~QID181~1');
// 'QR~QID181~1' is the ID of the first
// field in the constant sum question, other questions
// refered to other fields of the same question

   slider1.noUiSlider.on('end', function () { // I was using a custom slider
    inputNumber.value =1;    
   });
When all questions where answered the total sum should be equal to the amount of questions presented (in my case - 11).
The JS for the constant sum questions was as following:
Qualtrics.SurveyEngine.addOnload(function() {

//hide question
    jQuery("#"+this.questionId).hide();

       let that = this;
// disabling next button
      setTimeout(function(){ that.disableNextButton(); }, 1000);

// getting "total sum" field ID
        qc = that.questionContainer,
        id = that.questionId,
        total_box = qc.querySelector("#" + id + "_Total");

    total_box.oninput = function () {
        if ( total_box.value == 11) { // when sum is equal to the total amount of questions
// enable next button
that.enableNextButton();
// change next button text to arrow
var nb = jQuery('#NextButton');
nb.val("\\u2192");
nb.attr('title', newName);
}
}

// set next button text to custom message while disabled
var nb = jQuery('#NextButton');
nb.val("Answer all question to proceed");
nb.attr('title', newName);

});


Leave a Reply