Forum Discussion
Using JavaScript with True/False Buttons
Hello Walt and Math:
Thank you both for review. I did not proofread my post and can see how it was unclear. I have attached by Storyline file for review.
Essentially, when an item is picked, I would like it to appear in list form on a 'results' slide along with the item's nutritional values so that students can see their selections and proceed with questions following the exercise.
I've placed a table on my 'dessert' slide for the purposes of this post. In the example below, I have selected chocolate milkshake, fruit cup, and chocolate chip cookie. These items appear in the table along with their nutritional counts. This is exactly what I want to happen.
For each item, I have a combination of triggers and variables associated to a object checkbox (e.g. ChocChip).
Variables:
Triggers:
One Javascript trigger for the listing of items:
var player = GetPlayer();
var List_Dessert = player.GetVar("List_Dessert");
var ChocChip = player.GetVar("ChocChip");
var ChocChip_Button = player.GetVar("ChocChip_Button");
var Concate = List_Dessert + ChocChip;
player.SetVar("List_Dessert",Concate);
4 Javascript triggers for the listing of each nutritional value. Calorie listing example below:
var player = GetPlayer();
var List_Dessert_Calories = player.GetVar("List_Dessert_Calories");
var unitPrice = player.GetVar("ChocChip_Cal");
unitPrice = unitPrice.toFixed(0)
var displayPrice = unitPrice + "<br />";
var Concate = List_Dessert_Calories + displayPrice;
player.SetVar("List_Dessert_Calories",Concate);
The problem I am facing is, when items are unselected, they are added again to the table instead of being removed or not showing up. Here is an example below.
I don't know what the issue is so, your assistance is appreciated!
Lastly, referencing Math's comment:
Also be aware that variables in Storyline are always strings. So i do think the line in your code...
var Concate = List_Smash + Bacon;
is a bit weird. As i see it both List_Smash and Bacon are 2 text variables and all you do is adding them together. In fact the same as concat( ) does in Javascript but it would be good to use descriptive names as that makes life easier. So something like..
var breakfastString = List_Smash + Bacon;
would make more sense to me. You can always use this to detect what type a value is...
console.log("List_Smash is: "+typeof List_Smash);
I adapted all of my JS codes from this post: https://community.articulate.com/discussions/building-better-courses/building-a-text-list-using-variables-9076700d-32e3-4351-b09c-0a333ae871cf. So, if there is a better way of typing the JS command, please let me know.
Thank you both!
~ Christine
- MathNotermans-94 years agoCommunity Member
Thats quite a nice explanation Christine ;-) Out of the blue i guess the value isnot deleted from wherever its stored when deselecting it. If that was the case...whenever reselecting it again...it wouldnot matter it was added again... in fact you want that. Tomorrow morning im gonna check your file.