Have an idea?

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

Java script problem


I have an issue with a java script where I would appreciate your help.

Let’s say I have 2 questions: Q1 (Select, radio button) and Q2 (Numeric)

Q1 isn’t shown every time, depends on another question, let’s say Q0.

Q2 has to be shown every time. We need to put Java script condition: if Q1=2 and Q2=0 show error. We’ve did that and it works only when Q1 is shown. The problem is that when Q1 is not shown, Q2 is blocked (the script cannot continue no matter what input we put in Q2)    

if ( ([% q1 %] == 2) && ( SSI_GetValue("q2") == 0))

  strErrorMessage = "Ati spus mai devreme ca au fost si perioade in care nu ati avut x in stoc, suma nu poate fi 0";               

Best regards,
asked Feb 28, 2019 by Ciprian
Its working fine. Thank you very much for your time!

1 Answer

0 votes
The reason the code isn't working is because when the question is skipped, the value for Q1 is empty.  This means the resulting JavaScript is this:

if (( == 2) && (SSI_GetValue("q2") == 0))

JavaScript doesn't know how to handle that "( == 2)" so it crashes.  One workaround would be to put quotation marks around the Sawtooth Script, like this:

if (("[% q1 %]" == 2) && (SSI_GetValue("q2") == 0))

Now the two possible end results look like these, both of which are valid JavaScript:

if (("2" == 2) && (SSI_GetValue("q2") == 0))

if (("" == 2) && (SSI_GetValue("q2") == 0))
answered Feb 28, 2019 by Zachary Platinum Sawtooth Software, Inc. (212,550 points)