How can I manipulate Lectora User Variables in a Run JavaScript action?

Find out how to use Lectora User Variables in HTML Extensions and Run JavaScript Actions

Lectora Variables can be used within Lectora Actions, Conditionals, and Text Blocks to utilize dynamic content to drive branching and interactivity.

Sometimes you want to access these variable you defined to do some more sophisticated things using JavaScript.  Here is a quick example of how you can do just that.

Example problem:  I want to calculate a total for how many widgets I have if there are a defined number per box.

Lectora User Variables:  TotalWidgets, NumberWidgetsInBox, NumBoxes

I can do this in a Lectora Run JavaScript Action with the following code:

VarTotalWidgets.set( VarNumberWidgetsInBox.getValue() * VarNumBoxes.getValue() );

The names used in JavaScript for User Variables are the Lectora User Variable name prefixed with "Var"

To set a value you must call the method <variable>.set()

To get a value from a variable you call the method <variable>.getValue()

Once you can set and get Variables in JavaScript it opens up a whole new world for extending Lectora.  You can do anything in a Run JavaScript action or HTML Extension and then have the results available inside a Lectora User Variable for making branching (conditionals) decisions or to display results!