Have an idea?

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

Error Javascript

I have 2 question Q1 and Q10, both question are Select. I want to set confirm and error message as below.

var answered = [% Label(Q1) %];
if(SSI_GetValue("Q10") != SSI_GetValue("Q1")) 
    var confirmed = confirm('You answered ' + answered + '. Please re-confirm your answer.');
if (!confirmed) {strErrorMessage = 'Change answer and resubmit.';}

This script not working and I cannot find the error as this script working well with numeric questions.
asked Jun 22 by Saroeun Bronze (2,995 points)

2 Answers

0 votes
Best answer
You will need to implement Jay's change if your questions are on different pages, but if you look in your browser's console you'll see that there is something else breaking.  Whenever you use Sawtooth Script in your JavaScript, you should imagine replacing the Sawtooth Script with an example value that it might produce and decide whether or not that will become valid JS.  If Q1 were a numeric question, then this line:

var answered = [% Q1 %];

would become this JavaScript like this after the Sawtooth Script completes:

var answered = 123;

That's valid JavaScript and everything is fine.  But now imagine Q1 as a select question.  This line:

var answered = [% Label(Q1) %];

will become the label of the selected, like this:

var answered = hello world;

This is not valid JavaScript, causing your issue.  When we're sending text from Sawtooth Script to JS, we need to make the end result is a valid JavaScript string.  For simple cases, this can be done by just wrapping the Sawtooth Script with apostrophes or quotation marks.  So this line:

var answered = "[% Label(Q1) %]";

becomes the still valid JavaScript:

var answered = "hello world";
answered Jun 22 by Zachary Platinum Sawtooth Software, Inc. (206,100 points)
selected Jun 23 by Saroeun
Thanks for clearing that up Zach, I missed that.
0 votes
Which question are you applying this script to?  I am assuming Q10.  SSI_GetValue you use for a question on the same page as your script and [% %] for calling the value of a question on a previous page.
If my assumption above was correct, then this should be the way to call both values:

if(SSI_GetValue("Q10") != [% Q1 %])
answered Jun 22 by Jay Rutherford Platinum (50,145 points)
THanks, but still not working.