Copy link to clipboard
Copied
I'm spending an eternity trying to figure out how to perform a simple action, so I hope someone can help me.
I just want to use JavaScript to create a list of comma-separated text values, and then display one of these values (according to its position). I know how to display when there's only one value in a variable - but I think this requires an array.
Example:
var = animal
the values are: cat, dog, snake
and I want to
setVariableValue "animal" to display the second value ("dog").
I've been learning javascript, but can't adapt any of the examples to work for Captivate.
Sorry for the basic question. Thanks.
Bill
I had an error in the script, parens instead of braces in myArray[ 1 ]:
var myArray = [ "cat", "dog", "snake" ];
window.cpAPIInterface.setVariableValue( "animal", myArray[ 1 ] );
It works for me in both swf and html.
Copy link to clipboard
Copied
Hi Bill,
So I'm guessing you're asking the following:
1: How to write Javascript that saves the values cat, dog, and snake into an array.
2: How to write Javascript that gets one of those values out of an array and then writes it into a Captivate variable.
Is that correct?
Copy link to clipboard
Copied
Yes, that's basically it. I've been going through the Khan Academy course on arrays, but I can't figure out how to make them work in Captivate. I also tried to use the listbox widgets, but couldn't figure out how to get the comma separated values from those. I think an example might set me on the right path. Any suggestions?
Copy link to clipboard
Copied
What is your goal? Although JS can extend Captivate's functionality a lot, since you are talking about 'listbox' widget (old widget), maybe you can do this in a much easier way? Can you also please tell which CP-version you are using, and if you want to publish to SWF or to HTML? Because the listbox widget is SWF only.
Have a look at this article, where I explained how to use the List box widget:
http://blog.lilybiri.com/widgets-and-custom-questions-part-3
Copy link to clipboard
Copied
Hi Lilybiri,
I've looked through your posts pretty closely, and they've been helpful in the past, but am still at a loss un this case. I really need to use JS because I intend to do some other things with the arrays down the road. I mentioned the listbox, but I really don't need to use a widget. I just want to be able to parse a simple array for now, as in my example.
Copy link to clipboard
Copied
PS- I'm using CP9.
Copy link to clipboard
Copied
You want to be able to push items from an array into Captivate variables, using JS? Or the way round: pushing content of Captivate variables to an array? Or maybe both? Did you read this:
Because other experts are talking a lot about JS I keep focus in my blog on advanced/shared actions.
Copy link to clipboard
Copied
Hi Bill,
To help with this we really need to know if your project is being exported for HTML5 or for SWF.
Tristan,
Copy link to clipboard
Copied
Without knowing how you want it to be triggered you can execute this JavaScript:
var myArray = [ "cat", "dog", "snake" ];
window.cpAPIInterface.setVariableValue( "animal", myArray[1] );
This will populate "animal" with "dog";
Copy link to clipboard
Copied
Thanks - I want to trigger this with a click button on the same slide. I tried to preview your code both ways: "in browser" and "HTML5 in browser". I put the variable in a text box, and after the trigger it was blank for "in browser" (which i assume is swf?) and all three animal names appeared in html5. Do I need to reference CP above this code? If so- how should I do that?
At this point, I'd be happy to have it work for either swf or html5.
Thanks for everyone's help.
Copy link to clipboard
Copied
Did you define the variable 'animal' in Captivate? That is the name the JS is referencing to. That variable has to be inserted in a text container (is that what you mean by 'text box'?). API is same for SWF and HTML.
Copy link to clipboard
Copied
I had an error in the script, parens instead of braces in myArray[ 1 ]:
var myArray = [ "cat", "dog", "snake" ];
window.cpAPIInterface.setVariableValue( "animal", myArray[ 1 ] );
It works for me in both swf and html.
Copy link to clipboard
Copied
Fantastic. Thanks, it worked! Now if I want to show 2 values "dog" and "snake", does that require a loop? I tried:
var myArray = [ "cat", "dog", "snake" ];
window.cpAPIInterface.setVariableValue( "animal", myArray[ 1 ][ 2 ] );
But that just populated with the "g" at the end of dog (which is pretty interesting actually).
Copy link to clipboard
Copied
Your code represents a multi-dimensional array, you would need to use:
window.cpAPIInterface.setVariableValue( "animal", myArray[ 1 ] + ", " + myArray[ 2 ] );
This concatenates the two values separated by a comma and a space: dog, snake
Copy link to clipboard
Copied
Thanks again! I'll start a new thread with any new questions, but this gives me something to work with.
Copy link to clipboard
Copied
Sorry, David, but I had corrected your first answer for that small typo with the square brackets. Hope you didn't mind.
Arrays, random numbers and formatting of numbers are the three simple work flows that I'm using JS for as well...
Copy link to clipboard
Copied
Not a problem Lieve, thanks. I wondered how he even got it to work in HTML5.
Copy link to clipboard
Copied
Hi there David,
I was wondering how I could reset the array OnEnter.
If I take the above example, I would not want the "animal" to be populated with "dog", "cat" or anything. I wanted it to be for instance "choose an animal"
Thanks in advance
Bobby
Copy link to clipboard
Copied
Reset the array:
var myArray = ["", "", ""];
Not sure what you are using to display the "choose an animal" text.
You could put that text in 1 or all of the positions:
var myArray = ["choose an animal","choose an animal","choose an animal"]
Copy link to clipboard
Copied
Sorry
I should have explained more.
OnEnter I have this JS hooked
var profileCharacters = [ "", "Jamie", "John" ]
there are buttons with this attached to them
window.cpAPIInterface.setVariableValue("variableForProfile", profileCharacters[1]);
window.cpAPIInterface.setVariableValue("variableForProfile", profileCharacters[2]);
etc.
They would change the value.
The initial value is empty.
That is all good but when I go to the next slide and then come back to this slide, the variable has the last value assigned.
I want to reset it.
Thanks again
Bobby
Copy link to clipboard
Copied
window.cpAPIInterface.setVariableValue("variableForProfile","");
Copy link to clipboard
Copied
Thanks David,
This works.
So here is what I use. Do you think I should "polish" it a bit? Do I need all of that code?
I have this variable declared in Project/Variables: varProfileCharacter - it does not have any value.
I have also a smart shape with 3 states called "container_Character" and 2 of the states are Jamie and John.
On enter I have the following JS attached with "Current" and Continue Playing the Project ticked.
window.cpAPIInterface.setVariableValue("varProfileCharacter","");
var profileCharacters = [ "", "Jamie", "John" ]
On the button to change to profile 1 I have
window.cpAPIInterface.setVariableValue("varProfileCharacter", profileCharacters [1]);
cp.changeState("container_Character","Jamie");
On the button to change to profile 2 I have
window.cpAPIInterface.setVariableValue("varProfileCharacter", profileCharacters [2]);
cp.changeState("container_Character","John");
Does that seem neat?
It works well but I am just asking myself if there maybe too much of lines.
Thanks
Bobby
Copy link to clipboard
Copied
It looks fine to me. I don't know what you mean by "JS attached with 'Current'" though.
If you are resetting the variable, are you also resetting the state of the object that has the Characters?
Copy link to clipboard
Copied
Thanks David
The state would reset automatically unless I choose "Retain State on Slide Revisit"
AS to the JS attached with Current I mean that the Script window is Current, sorry
Thanks again
Copy link to clipboard
Copied
You're welcome.