Hide Question Based on Likert Response - JavaScript Help Needed | XM Community
Solved

Hide Question Based on Likert Response - JavaScript Help Needed

  • 1 December 2023
  • 28 replies
  • 277 views

Badge +2

Can anyone help me with the following JS code? only the first part is working. 
what i’m trying to do here (pardon my JS knowledge) is hide QID35 if the answer to QID34 is “Not applicable” 

given that QID34 is Matrix Table (likert) type and QID35 is multiple choice with force response and display logic for one of the statement

I don’t want to use display logic as it will create page break and the two questions must be in one page.

Qualtrics.SurveyEngine.addOnload(function() {
  // hide next question from start
    jQuery("#QID35").hide();
    
    // when current question is clicked
    this.questionclick = function(event, element) {
        
        // create variabe with clicked answer and check
        var selectedChoice = this.getChoiceRecodeValue()
        console.log(selectedChoice) 
        
        // show next question if condition is met
        if (selectedChoice !== "6") {
            jQuery("#QID35").show();}
        
        // in case answer is switched, hide again
         //   else{
           //     jQuery("#QID35").hide();
       // }
    }
});

Thanks in advance!

icon

Best answer by vgayraud 6 December 2023, 17:20

View original

28 replies

Userlevel 5
Badge +19

@vgayraudThank you so much for the code. it worked as expected, but upon testing, if the user clicked next button without answering the second question QID35, it hid the question and did not show the error message

the user have to click again on any radio button (except NA) so it show the second question
any idea how to fix that?
thanks,

You can use this below updated code to account for  the above scenario:

 

Qualtrics.SurveyEngine.addOnReady(function()
{


if (this.getSelectedAnswerValue(1) !== "6" && this.getSelectedAnswerValue(1) !== null )
{
jQuery("#QID35").show();
}
else {
jQuery("#QID35").hide();
}



this.questionclick = function(event, element) {
if (this.getSelectedAnswerValue(1) !== "6" && this.getSelectedAnswerValue(1) !== null ) { jQuery("#QID35").show();
}
else {
jQuery("#QID35").hide();
}
}
});


Hope this resolves your query😊!!

Userlevel 5
Badge +19

Hi @Fatma ,
Please use below updated code to accommodate above scenario:

 



Qualtrics.SurveyEngine.addOnReady(function()
{


if (this.getSelectedAnswerValue(1) !== "6" && this.getSelectedAnswerValue(1) !== null )
{
jQuery("#QID35").show();
}
else {
jQuery("#QID35").hide();
}



this.questionclick = function(event, element) {
if (this.getSelectedAnswerValue(1) !== "6" && this.getSelectedAnswerValue(1) !== null )
{ jQuery("#QID35").show();
}
else {
jQuery("#QID35").hide();
}
}

});


Hope this resolves your query😊!!

Userlevel 6
Badge +39

Hi,

Adapt the first part of the code with @qualtrics_nerd ‘s answer. You need to check if a valid answer is checked before hiding the second question.

Leave a Reply