Accessing variables in an external javascript file with Lectora

smcculley1282 Community Member Posts: 14 ☆ Roadie ☆

I am creating an assessment quiz that will eventually have 9 variations which is why all the text for the questions and answers is in one javascript file. I am able to access the text for the question and answers in the javascript file just fine. The question I have is on the very last page of the assessment, "Feedback." We want to be able to put the text of each question along with other information into a table. On the first line of the table, I have the Lectora variable VAR(mytest), which right now returns "0." What I want to do here is to the put the value (or text) of the variable "Q1" (in the external javascript file AssessmentQText.js, line 26) into the VAR(mytest) variable and have that text appear in row 1, cell 2 of the table. Is this possible? If so, what is the correct syntax to make this work?

Any help with this is much appreciated. My Lectora project file is attached.




  • timk
    timk Community Member Posts: 1,236 ✭ Legend ✭
    Inline variable replacement with "VAR(mytest)" has been designed to insert the page name into a textfield so fast, a user cannot see the default text of the field. So this is what happens first when the page is loaded and it happens only once.

    All actions to modify that variable are executed later, i.e. the action to set the variable to "8" is working but it happens after the variable has been inserted into the textfield, when its value is still equal to "0". It should work if you modify the variable on a previous page.

    The code to set a Lectora variable with javascript is: Varvariablename.set("Value");

    For testing you can add it to your function updateQuestion1()


    Be aware that you need a Lectora action that uses the variable "mytest" on the question page. Otherwise it won't exist. The easiest way to achieve it is to add a Group on Chapter level with one action to "Reset all variables".
  • smcculley1282
    smcculley1282 Community Member Posts: 14 ☆ Roadie ☆
    Hi Tim. Thank you for your quick rely. Your answer worked and I am able to correctly access the variables in my javascript file.