Have an idea?

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

Javascript in list building

Trying to use specific text in a list build based on the value of a variable. Using a numeric variable works fine but having problems using a text variable.
This work...
Used a [%Value(brand)%] ATM (please exclude ATMs that are not [%Value(brand)%]’s
<script>
if ([%brandnum%] == 1)
document.write("; include [%Value(brand)%] ATMs that had a live teller available");
</script>
)


This does not work (assume the value of brand is ABC)...
Used a [%Value(brand)%] ATM (please exclude ATMs that are not [%Value(brand)%]’s
<script>
if ([%brand%] == "ABC")
document.write("; include [%Value(brand)%] ATMs that had a live teller available");
</script>
)


When I view the resulting page code, the conditional reads...
if ABC == "ABC"
but the document.write command is not executed.
asked May 15 by Chris Guth Bronze (780 points)

1 Answer

+1 vote
 
Best answer
If you look in your browser's console on that page, you may see an exception to this effect: "ABC is not defined."  The JavaScript thinks the first part of that equality check is a variable, so it tries to find a variable by that name and then breaks down when it can't find it.  To prevent this, we have to use quotation marks when passing text from Sawtooth Script to JavaScript:

if ("[% brand %]" == "ABC") {
    ...
}


The Sawtooth Script will still run the same as always, but now the value will be treated like a string once it makes it to JavaScript.
answered May 15 by Zachary Platinum Sawtooth Software, Inc. (120,400 points)
selected May 15 by Chris Guth
ahhhhh...thank you!
...