Featured Categories
-
Lectora®
Lectora® is the most trusted authoring tool in the world. Rock out your creativity! Author courses any way you like. Lectora adheres to best practices for Accessibility and Web Content Accessibility Guidelines (WCAG), offers responsive authoring for eLearning, and publishes to any LMS (Learning Management System).
-
CenarioVR®
Experience virtual reality through CenarioVR®, our training and virtual reality (VR) authoring software that allows you to create immersive learning in minutes! No coding required!
-
Rockstar Learning Platform®
Our Rockstar Learning Platform® has all the features you’ll love. Help your employees be better at their jobs, more valuable to your company, and most effective in servicing your customers.
-
The Training Arcade®
Level up your learner engagement with the Training Arcade®! The Training Arcade is a library of fun, mobile-ready, casual games that can be rapidly customized with your content to create effective gaming!
-
ELB Learning Content
Did you know that our AssetLibrary™ has over 120 million engaging, interactive, and awe-inspiring templates and assets to help you become an eLearning Rockstar®?
Getting javascript to talk to lectora variables

So we're using javascript to evaluate the answers given for the X number of Must Be Correct questions and working out the percentage. But no matter how we try, we cannot get the javascript to talk to the variable we have created in Lectora to receive the percentage (which will be used later to post to a result screen).
We've seen lots of "this is how to do it" posts but none actually work. Can someone post a really simple set of instructions on how to communicate with lectora variables from javascript, either in or out of a function?
many thanks
frank
Comments
-
So you have created a custom variable called "Greeting". This is the script to get itsvalue:
var gr = VarGreeting.getValue();
..and to set variable:
VarGreeting.set("hello");
That's it. It works. If it isn't working for you, you are doing something wrong. Post a sample if you want someone to look at it. -
@ssneg 61829 wrote:
So you have created a custom variable called "Greeting". This is the script to get itsvalue:
var gr = VarGreeting.getValue();
..and to set variable:
VarGreeting.set("hello");
That's it. It works. If it isn't working for you, you are doing something wrong. Post a sample if you want someone to look at it.
Hi there - thanks for the reply; ok we got 50% of this working.
We managed to set Lectora internal variables using .set and even managed a percentage calculation in the brackets to push an evaluated number into the variable.
Getting the variables from Lectora is proving more tricky.
Using the following script causes Lectora not to populate the VarSecPercentage variable:
var secTotalScore = VarSec_Question_1.getValue();
VarSecPercentage.set(secTotalScore/3*100);
[/CODE]
If we try just
[CODE]
VarSecPercentage.set(3/6*100);
[/CODE]
for example hard coding the numbers in, we get 50.....voila working.
If we try
[CODE]
var secTotalScore = VarSec_Question_1.getValue()+VarSec_Question_2.getValue()+VarSec_Question_3.getValue();
VarSecPercentage.set(secTotalScore/3*100);
[/CODE]
again nothing.
So Lectora doesn't like the first line, either just a single variable call or multiple and adding them up.
To me this should just be basic Javascript but Lectora is throwing added spanners in the works with the use of Var and .set and .getValue.
Thanks for any help
Frank[CODE]
var secTotalScore = VarSec_Question_1.getValue();
VarSecPercentage.set(secTotalScore/3*100);
[/CODE]
If we try just
VarSecPercentage.set(3/6*100);
[/CODE]
for example hard coding the numbers in, we get 50.....voila working.
If we try
[CODE]
var secTotalScore = VarSec_Question_1.getValue()+VarSec_Question_2.getValue()+VarSec_Question_3.getValue();
VarSecPercentage.set(secTotalScore/3*100);
[/CODE]
again nothing.
So Lectora doesn't like the first line, either just a single variable call or multiple and adding them up.
To me this should just be basic Javascript but Lectora is throwing added spanners in the works with the use of Var and .set and .getValue.
Thanks for any help
Frank[CODE]
VarSecPercentage.set(3/6*100);
[/CODE]
for example hard coding the numbers in, we get 50.....voila working.
If we try
var secTotalScore = VarSec_Question_1.getValue()+VarSec_Question_2.getValue()+VarSec_Question_3.getValue();
VarSecPercentage.set(secTotalScore/3*100);
[/CODE]
again nothing.
So Lectora doesn't like the first line, either just a single variable call or multiple and adding them up.
To me this should just be basic Javascript but Lectora is throwing added spanners in the works with the use of Var and .set and .getValue.
Thanks for any help
Frank[CODE]
var secTotalScore = VarSec_Question_1.getValue()+VarSec_Question_2.getValue()+VarSec_Question_3.getValue();
VarSecPercentage.set(secTotalScore/3*100);
[/CODE]
again nothing.
So Lectora doesn't like the first line, either just a single variable call or multiple and adding them up.
To me this should just be basic Javascript but Lectora is throwing added spanners in the works with the use of Var and .set and .getValue.
Thanks for any help
Frank -
Frank, Lectora variables are strings. Even if it contains a number, it's still a string. Dividing a string by a number results in error. Adding two strings like "4" + "12" produces "412", not 16.
So instead of this:
var Res = VarOne.getValue(); // it is "3", i.e. string
do this:
var Res = parseInt(VarOne.getValue()); // it is 3, i.e. number
Then you can manipulate your Res as number (divide, multiple, add). -
You can do all this in Lectora w/o JavaScript. If they are questions, the question varible has the answer choice selected, not the value or weight of the question in the overall score. Generally unless you change it, questions are weighted 1 pt. So you have to test the question for correctness and add 1 to a total if it is correct.
Ihn Lectora it would look something like this:
Modify Variable _must_Score Set 0
Modify Variable _must_Score Add 1; Condition: Question_0001 Is Correct
Modify Variable _must_Score Add 1; Condition: Question_0002 Is Correct
Modify Variable _must_Score Add 1; Condition: Question_0003 Is Correct
Modify Variable _must_Score Divide 3
Modify Variable _must_Score Multiply 100
Now it is maintainable by a Lectora developer who does not know JavaScript. -
@ssneg 61838 wrote:
Frank, Lectora variables are strings. Even if it contains a number, it's still a string. Dividing a string by a number results in error. Adding two strings like "4" + "12" produces "412", not 16.
So instead of this:
var Res = VarOne.getValue(); // it is "3", i.e. string
do this:
var Res = parseInt(VarOne.getValue()); // it is 3, i.e. number
Then you can manipulate your Res as number (divide, multiple, add).
Hi there - ok; we have tried -
var SecTotalScore = parseInt(VarSec_Question_1.getValue());
VarSecPercentage.set(SecTotalScore);
[/CODE]
This should simply grab the score, as a string, convert it to an interger and then set the variable VarSecPercentage as that integer (in this case 1). The text area is not changed.
If we do
[CODE]
//var SecTotalScore = parseInt(VarSec_Question_1.getValue());
VarSecPercentage.set(50);
[/CODE]
50 appears in the text box. So we can set the variable ok. And we can apply maths within .set() ok, so that all works. So it must be either a) we're not grabbing the variable from Lectora correctly or b) an error is occuring during parseInt.
Thanks for the suggestions - including Bens; the reason we're using javascript is that this test has say 100 questions. Within the 100 questions there are 10 you must get 100% right. So this is a separate javascript test to ensure those 10 are score 100% before the rest of the test is processed internally in Lectora.....convolted yes, but clients are clients
So breaking it down -
a) I assume I'm creating a javascript temp variable correctly - var SecTotalScore?
b) =parseInt(); conducts an interger change on any strings and passes it into SecTotalScore?
c) VarSec_Question_1.getValue() gets me a string of the score given for the answer to Sec_Question_1?
d) .set() accepts Javascript variables as parameters?
If yes to all of these, then what is going wrong if
VarSecPercentage.set(50); passes 50,
VarSecPercentage.set(3/6*100); passes 50,
but
var SecTotalScore = parseInt(VarSec_Question_1.getValue());
VarSecPercentage.set(SecTotalScore);
does nothing
We're fairly new to Lectora and although not new to javascript, Lectoras implimentation of it is throwing a few spanners in the works!
cheers
frank[CODE]
var SecTotalScore = parseInt(VarSec_Question_1.getValue());
VarSecPercentage.set(SecTotalScore);
[/CODE]
This should simply grab the score, as a string, convert it to an interger and then set the variable VarSecPercentage as that integer (in this case 1). The text area is not changed.
If we do
//var SecTotalScore = parseInt(VarSec_Question_1.getValue());
VarSecPercentage.set(50);
[/CODE]
50 appears in the text box. So we can set the variable ok. And we can apply maths within .set() ok, so that all works. So it must be either a) we're not grabbing the variable from Lectora correctly or b) an error is occuring during parseInt.
Thanks for the suggestions - including Bens; the reason we're using javascript is that this test has say 100 questions. Within the 100 questions there are 10 you must get 100% right. So this is a separate javascript test to ensure those 10 are score 100% before the rest of the test is processed internally in Lectora.....convolted yes, but clients are clients
So breaking it down -
a) I assume I'm creating a javascript temp variable correctly - var SecTotalScore?
b) =parseInt(); conducts an interger change on any strings and passes it into SecTotalScore?
c) VarSec_Question_1.getValue() gets me a string of the score given for the answer to Sec_Question_1?
d) .set() accepts Javascript variables as parameters?
If yes to all of these, then what is going wrong if
VarSecPercentage.set(50); passes 50,
VarSecPercentage.set(3/6*100); passes 50,
but
var SecTotalScore = parseInt(VarSec_Question_1.getValue());
VarSecPercentage.set(SecTotalScore);
does nothing
We're fairly new to Lectora and although not new to javascript, Lectoras implimentation of it is throwing a few spanners in the works!
cheers
frank[CODE]
//var SecTotalScore = parseInt(VarSec_Question_1.getValue());
VarSecPercentage.set(50);
[/CODE]
50 appears in the text box. So we can set the variable ok. And we can apply maths within .set() ok, so that all works. So it must be either a) we're not grabbing the variable from Lectora correctly or b) an error is occuring during parseInt.
Thanks for the suggestions - including Bens; the reason we're using javascript is that this test has say 100 questions. Within the 100 questions there are 10 you must get 100% right. So this is a separate javascript test to ensure those 10 are score 100% before the rest of the test is processed internally in Lectora.....convolted yes, but clients are clients
So breaking it down -
a) I assume I'm creating a javascript temp variable correctly - var SecTotalScore?
b) =parseInt(); conducts an interger change on any strings and passes it into SecTotalScore?
c) VarSec_Question_1.getValue() gets me a string of the score given for the answer to Sec_Question_1?
d) .set() accepts Javascript variables as parameters?
If yes to all of these, then what is going wrong if
VarSecPercentage.set(50); passes 50,
VarSecPercentage.set(3/6*100); passes 50,
but
var SecTotalScore = parseInt(VarSec_Question_1.getValue());
VarSecPercentage.set(SecTotalScore);
does nothing
We're fairly new to Lectora and although not new to javascript, Lectoras implimentation of it is throwing a few spanners in the works!
cheers
frank -
@elfanko 61840 wrote:
c) VarSec_Question_1.getValue() gets me a string of the score given for the answer to Sec_Question_1?
I assume "Sec_Question_1" is the variable name for the question? If so, it will contain the chosen answer text to that question. E.g. if the question is "What color is the sky?" with option "Red" and "Blue" and quiz point value of 10 points, the variable will contain "Red" or "Blue", not "10".
It's quite easy to see and understand all your errors if you look into the browser's console and check out the errors (like dividing a string by a number). Or use console.log JS command to output and check variable values. Or use alert() to the same effect. It will save you a lot of time. -
@benpitman 61839 wrote:
You can do all this in Lectora w/o JavaScript. If they are questions, the question varible has the answer choice selected, not the value or weight of the question in the overall score. Generally unless you change it, questions are weighted 1 pt. So you have to test the question for correctness and add 1 to a total if it is correct.
Yep, as much as I like JavaScript, this is exactly what I'd do in this case.
Ihn Lectora it would look something like this:
Modify Variable _must_Score Set 0
Modify Variable _must_Score Add 1; Condition: Question_0001 Is Correct
Modify Variable _must_Score Add 1; Condition: Question_0002 Is Correct
Modify Variable _must_Score Add 1; Condition: Question_0003 Is Correct
Modify Variable _must_Score Divide 3
Modify Variable _must_Score Multiply 100
Now it is maintainable by a Lectora developer who does not know JavaScript. -
Hi guys - thanks for your help on this; this is now resolved.
Yes we found just before checking this thread that the variable for the question contains the answer given not the score....d'oh - lesson learned.
So then we went through Bens solution and it worked perfectly. I was trying to solve the equation through a programmers eyes with my content creator colleague. Bens solution solves the problem completely, I just couldn't see it being a programmer
kind regards to all involved
frank -
Categories
- 35.9K All Categories
- 110 ✫ Announcements
- 33.2K Lectora®
- 31.1K Lectora Discussions
- 29K Lectora Desktop
- 2K Lectora Online
- 2K Lectora Feature Requests
- 71 Lectora User Groups
- 36 Lectora Accessibility User Group (LAUG)
- 27 ELB Learning Content
- 27 ELB Learning Content Discussions
- 346 CenarioVR®
- 205 CenarioVR Discussions
- 141 CenarioVR Feature Requests
- 44 Rockstar Learning Platform®
- 41 Rockstar Learning Platform Discussions
- 108 CourseMill®
- 108 CourseMill Discussions
- 48 ReviewLink®
- 48 ReviewLink Discussions
- 7 The Training Arcade®
- 7 The Training Arcade Discussions
- 938 All Things eLearning
- 39 eLearning Development
- 546 Learning Management System (LMS) Integration
- 333 Web Accessibility
- 1.2K ♪ The Green Room
- 9 Additional Learning Products