Clearing answers from one question based on the answer to another | XM Community
Skip to main content

Hello,

 

There are 3 single choice questions which I  number 1, 2 and 3. Question 1 has two possible answers A and B.

I am trying to write a java script such that:

  1. question 2 is displayed only if question 1 answer is A.
  2. question 3 is displayed only if question 1 answer is B.
  3. Questions 2 or question 3 are displayed on the same page as question 1.
  4. If a respondent changes answer to question 1 from A to B, the previously provided answer to question 2 clears. Similarly, if a respondent changes answer to question 1 from B to A, the previously provided answer to question 3 clears

I can do parts i, ii, and iii but cannot figure out part iv. 

 

I circling around the following java code for question 2:

 

Qualtrics.SurveyEngine.addOnReady(function()
{
jQuery("#QID1").on('change', function(){    
    
var LegChoices = "${q://QID1/SelectedChoicesRecode}";
var that = this;
LegChoices.split(",").each(function(LegChoice,index) {
    var LegChoice = parseInt(LegChoice.trim());
    if (LegChoice!=1) {
        that.setChoiceValueByRecodeValue(1,false);
        that.setChoiceValueByRecodeValue(2,false);

        that.setChoiceValueByRecodeValue(3,false);

        that.setChoiceValueByRecodeValue(4,false);

        that.setChoiceValueByRecodeValue(5,false);
        that.prop("checked", false)
    }
})
});
})

@martaboczon,

Have you tried using in-page display logic on questions 2 and 3? It would be a lot easier.

One of the reasons your JS isn’t working is because you are piping the value of QID1. Piped values are resolved before the page is sent to the browser, so it will always be blank.


Dear @TomG , thank you for your reply. I am using in-page display logic. The problem I am facing is more nuance:

 

Imagine you choose option A in question 1 and question 2A is displayed using in-page display logic. You chose an answer to question 2A but instead of clicking on the next button you hesitate: you go back to question 1 and choose answer B so that question 2B is displayed (again, using in-page display logic). Here yet again you hesitate one more time and change your answer in question 1 back to A. When that happens question 2A is displayed as it should but it displays with a checked choice from before to question 2A. What I want to do but do not know how is to not have any previously checked answers show up when a question number 2 is displayed. I want the system to erase that choice. Do you have an idea of how to do that?


@martaboczon,

The general idea of your JS is correct, but has the following issues:

  1. You should be using a click event instead of a change event
  2. You can’t pipe the value from the first question - you need to check which radio button is checked
  3. You don’t need split
  4. You should be using the setChoiceAnswerValue function in a loop to uncheck choices

 

 


Dear @TomG, thank you.

This is where I am now, still not working but I think I at least addressed some of the issues you raised. Am I getting there?

 

Qualtrics.SurveyEngine.addOnReady(function()
{
    jQuery("#QID1").on('click', function(){      
        var selected = jQuery('#'+this.questionId).find(".QuestionBody").findotype = "checkbox"];
        var selectedid = selected.filter(":checked");
        selectedid.each(function(index) {
           selectedid.setChoiceAnswerValue(index, false);
        })
    });
})


@martaboczon,

A few things:

  1. You said you had single select questions - those would be [type=radio]
  2. You need to check if the choice of interest is not selected in QID1, not the current question
  3. Loop through all the choices in the current question (i.e., jQuery('#'+this.questionId+" [type=radio]");
  4. The first argument of setChoiceAnswerValue() is the choice id (not the same as the loop index)

Dear @TomG,

 

The code is finally working!

 

Qualtrics.SurveyEngine.addOnReady(function() {
    var question1Id = 'QID178'; // Replace with the actual question ID of Question 1
    var question2Id = 'QID182'; // Replace with the actual question ID of Question 2

    // Listen for clicks on Question 1 radio inputs
    jQuery("#" + question1Id + " .radio").on('change', function() {
        // Uncheck all radio inputs in Question 2
        Qualtrics.SurveyEngine.registryuquestion2Id].setChoiceValue(1, false);
        Qualtrics.SurveyEngine.registryrquestion2Id].setChoiceValue(2, false);
        Qualtrics.SurveyEngine.registryquestion2Id].setChoiceValue(3, false);
        Qualtrics.SurveyEngine.registryquestion2Id].setChoiceValue(4, false);
        Qualtrics.SurveyEngine.registryquestion2Id].setChoiceValue(5, false);
        Qualtrics.SurveyEngine.registrybquestion2Id].setChoiceValue(6, false);
        Qualtrics.SurveyEngine.registrylquestion2Id].setChoiceValue(7, false);
        Qualtrics.SurveyEngine.registry(question2Id].setChoiceValue(8, false);
        Qualtrics.SurveyEngine.registryequestion2Id].setChoiceValue(9, false);
        Qualtrics.SurveyEngine.registrytquestion2Id].setChoiceValue(10, false);
    });
   jQuery("#" + question2Id + " .radio").on('click', function() {
jQuery("#NextButton").show();
           });
});


@martaboczon

Congrats! An improvement would be to make it general purpose without hardcoded QIDs or choice ids. The Next button bit might not be needed.


Leave a Reply