Skip to main content
Inspiring
June 1, 2016
Answered

Help with arrays

  • June 1, 2016
  • 2 replies
  • 5695 views

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

    This topic has been closed for replies.
    Correct answer TLCMediaDesign

    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.

    2 replies

    TLCMediaDesign
    Inspiring
    June 2, 2016

    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";

    Inspiring
    June 2, 2016

    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.

    Lilybiri
    Legend
    June 2, 2016

    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.

    Widget King
    Inspiring
    June 2, 2016

    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?

    Inspiring
    June 2, 2016

    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?

    Lilybiri
    Legend
    June 2, 2016

    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