How to reset multiple Question | XM Community

How to reset multiple Question

  • 5 December 2022
  • 2 replies
  • 66 views

Badge +2

Hi Community,
I'm trying to work with can add a reset/clear option in the multiple question in single page that would like to clear their selections for the current question, they can simply press a reset/clear button and it would delete all the responses for that question at once
I tried this code; however, it's only working single question in single page and has an error when I click this clear/reset button it disables the validation and when I back it appears the last selection that I already reset/clear.
Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/
const quest = this;
    const qc = quest.questionContainer;
var questionId = this.questionId;
var inputs =  $(this.getQuestionContainer()).select('input[type="radio"]', 'input[type="checkbox"]', 'input[type="text"]');


    qc.querySelector(".QuestionText").insertAdjacentHTML("afterend", "");


    qc.querySelector(".reset").onclick = function () {

       for (var i = 1; i < inputs.length; i++) {
            Qualtrics.SurveyEngine.registry[questionId].setChoiceValue(i, false);
     qc.querySelectorAll(".InputText").forEach((item) => {
            item.value = "";

});
}

for (var i = 1; i < inputs.length; i++) {
            Qualtrics.SurveyEngine.registry[questionId].setChoiceValue(i, false);
      qc.querySelectorAll(".q-checked").forEach((item) => {
            item.classList.remove("q-checked");
qc.querySelectorAll("select").forEach((item) => {
            item.clear();
});
        });
}
    }
});




2 replies

Badge +1

Sorry, Commented on wrong post. Can't delete now!

Userlevel 1
Badge +6

Hi @ricsdpl 

 

i dont know if you still need a answer.

 

But i try it with this code and it worked

 

Qualtrics.SurveyEngine.addOnload(function() {
    const quest = this;
    const qc = quest.questionContainer;
    var questionId = this.questionId;
    var inputs = qc.querySelectorAll('input[type="radio"], input[type="checkbox"], input[type="text"]');
    
    qc.querySelector(".QuestionText").insertAdjacentHTML("afterend", "<button class='reset'>Clear Responses</button>");
    
    qc.querySelector(".reset").onclick = function() {
        for (var i = 0; i < inputs.length; i++) {
            inputs[i].checked = false;
            if (inputs[i].type === "text") {
                inputs[i].value = "";
            }
        }
        qc.querySelectorAll(".q-checked").forEach((item) => {
            item.classList.remove("q-checked");
        });
        qc.querySelectorAll("select").forEach((select) => {
            select.selectedIndex = -1; // Clear selected option
        });
    };
});
 

Leave a Reply