Hiding column#2 in a side-by-side Question, and revealing it upon selection of choices in column#1 | Experience Community
Skip to main content
Solved

Hiding column#2 in a side-by-side Question, and revealing it upon selection of choices in column#1

  • Mar 26, 2021 4:45 PM UTC
  • 5 replies
  • 515 views

Dear All,
With reference to the picture below, I would like to hide column #2 when the survey enters into the page, and to reveal column#2 when choices in column #1 are selected (either YES&YES or NO&NO).
Initially, I tried to apply the display logic on the question for column 2. It did hide the column, but did not reveal it when the conditions are met. After some readings, it seems that display logic does not work well with SBS question. Hence, I have attempted to add JS into this question.
image.pngLogic: hide column#2, get the recode value of choices in column#1, check condition, reveal column#2 if conditions are met.
I do not have any prior JS knowledge, but have tried to piece together solutions offered in this forum. Below is my current JS code.
image.pngAppreciate if someone could help please. Thank you.

Best answer by ahmedA

Okay. Yes that's an issue. Sorry didn't notice that I was hiding SBS2. Just delete the else block:
else { // Hide SBS2 otherwise
                quest.questionContainer.querySelectorAll(".SBS2").forEach((item) => item.hide());
}
And it should work fine.

View original

5 replies

Forum|alt.badge.img+22
  • Level 7 ●●●●●●●
  • Mar 26, 2021 6:35 PM UTC

Use this JS:
Qualtrics.SurveyEngine.addOnReady(function () {
    let quest = this;
    
    // Hide SBS2 to start with
    quest.questionContainer.querySelectorAll(".SBS2").forEach((item) => item.hide());

    quest.questionContainer.onclick = function () {
        setTimeout(() => {
            let all_checked = quest.questionContainer.querySelectorAll(".q-checked");
            
            // Show SBS2 if both are the same
            if (
                all_checked.length == 2 &&
                all_checked[0].parentElement.className == all_checked[1].parentElement.className
            ) {
                quest.questionContainer.querySelectorAll(".SBS2").forEach((item) => item.show());
            } else { // Hide SBS2 otherwise
                quest.questionContainer.querySelectorAll(".SBS2").forEach((item) => item.hide());
            }
        }, 100);
    };
});

You may want to explore some aesthetic related customizations also.


  • Author
  • Mar 27, 2021 2:26 AM UTC

Thank you so much, ahmedA. Your solution is able to hide and show column#2 according to the conditions.
However, it hides column#2 again when I select one of the radio buttons. I tried to adjust the first condition (please see below), but I not sure how to restrict the second condition to only column#1.
Could you advise please? Thank you!
Qualtrics.SurveyEngine.addOnReady(function () {
    let quest = this;
    
    // Hide SBS2 to start with
    quest.questionContainer.querySelectorAll(".SBS2").forEach((item) => item.hide());

    quest.questionContainer.onclick = function () {
        setTimeout(() => {
            let all_checked = quest.questionContainer.querySelectorAll(".q-checked");
            
            // Show SBS2 if both are the same
            if (
                all_checked.length >= 2 &&
                all_checked[0].parentElement.className == all_checked[1].parentElement.className
            ) {
                quest.questionContainer.querySelectorAll(".SBS2").forEach((item) => item.show());
            } else { // Hide SBS2 otherwise
                quest.questionContainer.querySelectorAll(".SBS2").forEach((item) => item.hide());
            }
        }, 100);
    };
});



Forum|alt.badge.img+22
  • Level 7 ●●●●●●●
  • Answer
  • Mar 27, 2021 11:39 AM UTC

Okay. Yes that's an issue. Sorry didn't notice that I was hiding SBS2. Just delete the else block:
else { // Hide SBS2 otherwise
                quest.questionContainer.querySelectorAll(".SBS2").forEach((item) => item.hide());
}
And it should work fine.


  • Author
  • Mar 27, 2021 1:15 PM UTC

Hi ahmedA,
Yes, it works fine. Thanks a lot!


Forum|alt.badge.img
  • Oct 12, 2021 8:00 PM UTC

Very nice code. Thank you!