Have an idea?

Visit Sawtooth Software Feedback to share your ideas on how we can improve our products.

Required response only if shown question (Branching)

I'm using the code from  the Branching question submitted in the Question Library to hidde/show a question based on the answer to another question on the same page: https://www.sawtoothsoftware.com/community-question-library/1731-branching

In my case, Q2 needs to be shown if Q1=3. That works well.  The problem is that Q2 is automatically required, even if it is hidden, so if Q1<3, I get an error on the page and I can't proceed.

The question library notes say: Custom JavaScript verification can be used to make questions require a response only if shown. This code may help you get started:
 $('#[% QuestionName() %]_div').is(':visible');


I added that code to the Custom JavaScript verification window for Q2, but I still get the error. I need Q2 to be required, only when shown. What am I missing?
asked Dec 5, 2019 by rinsights Bronze (890 points)
That snippet can potentially be used in creating the behavior you are after, but it is not complete code.  The exact finalized code needed is going to depend on Q2:

* What type of question is Q2?  Select, numeric, open-end, etc.?
* If it's a select or grid question, what input type does it use?  Radio buttons, checkboxes, etc.?
* If it's a grid question, is it row-based or column-based?
Q2 is open-ended.
got the same problem with dropdown in Q2. I can't proceed if Q1 is selected with NO. Because Q2 only appears if Q1 is YES.

How to fix this?

1 Answer

0 votes
Set Q2 to not require a response and give it this custom JavaScript verification:

if (SSI_GetValue('Q1') == 3 && !SSI_GetValue('Q2')) {
    strErrorMessage = 'Response is required.';
}
answered Dec 5, 2019 by Zachary Platinum Sawtooth Software, Inc. (207,300 points)
Thanks. That code doesn't work.
However, I found another post saying to use this code for what seems to be a select question:
if (SSI_GetValue('Q1') == 3 && SSI_GetValue('Q2') == 0) {
    strErrorMessage = 'Error message here';
}

When I use this code and make Q2=0, it works, which is weird because my Q2 is an open-ended question. Why would this work with an open-ended question?
That code ought to work.  If you can email your misbehaving .ssi in to support@sawtoothsoftware.com, we may be able to find what's going wrong.

JavaScript is a dynamically-typed language.  What this means is that it plays a little fast-and-loose when comparing data of different types, converting one data type into another on the fly.  For example, all of these return true in JS:

0 == 0
"" == 0
"0" == 0
[] == 0


That second or third one is probably what you are observing.

If you want to do comparisons in JavaScript without these implicit conversions, you need to use the "===" and "!==" operations instead of the "==" and "!=" operators.  Only the first of these returns true:

0 === 0
"" === 0
"0" === 0
[] === 0
Thanks for the explanation, Zach. I found the problem. I was missing the exclamation mark in front of the condition for Q2.
...