Skip to main content
justinm53961168
Inspiring
May 4, 2016
Answered

showing random tips (not questions)

  • May 4, 2016
  • 3 replies
  • 296 views

I've read through the forum and I've seen talk about random questions from slides, but that is not my need.

I need to create a pool of tips that will generate at random when a button is clicked. A user clicks a box "Quick Tip" and it show Tip1, they click again it shows Tip2, etc.

anyone done something like this?

    This topic has been closed for replies.
    Correct answer TLCMediaDesign

    As I said, for the script to work it had to be placed in the head of the html page, not the JS window.

    I answered the other thread with the code to make it work in the JS window.

    My thought was that there would be a ton of text and the JS window is very small.

    3 replies

    TLCMediaDesign
    Inspiring
    May 5, 2016

    I think this line should be changed to:

    var newTip = Math.floor(( Math.random() * myTips.length - 1 ) + 0 );

    justinm53961168
    Inspiring
    May 5, 2016

    hey TLCMedia...thanks for your help. I've done as instructed but can't seem to get it to work.

    This is the code I have:

    <script>

    var myTips = [];

    myTips[ 0 ] = 'This is the text for tip one.';  

    myTips[ 1 ] = 'This is the text for tip two.';

    myTips[ 2 ] = 'This is the text for tip three.';

    myTips[ 3 ] = 'This is the text for tip four.';

    function getTips()

    {

    var newTip = Math.floor(( Math.random() * myTips.length -1 ) + 0 );

    window.cpAPIInterface.setVariableValue( 'tip', myTips[ newTip ] );

    }

    </script>

    here is a screenshot:

    TLCMediaDesign
    TLCMediaDesignCorrect answer
    Inspiring
    May 5, 2016

    As I said, for the script to work it had to be placed in the head of the html page, not the JS window.

    I answered the other thread with the code to make it work in the JS window.

    My thought was that there would be a ton of text and the JS window is very small.

    TLCMediaDesign
    Inspiring
    May 4, 2016

    This would be fairly simple with JavaScript. Create a text caption with a variable as it's contents.

    It would be best to include a JavaScript file since the script could get long. Or you could put it in the head of the html page

    Create an array of your tips.

    Click the button executing getTips()

    the function returns a random number to use as an index for the array and populates the CP variable with the text.

    <script>
    var myTips = [];

    myTips[ 0 ] = 'This is the text for tip one.';  
    myTips[ 1 ] = 'This is the text for tip two.';
    myTips[ 2 ] = 'This is the text for tip three.';
    myTips[ 3 ] = 'This is the text for tip four.';

    function getTips()
    {
    var newTip = Math.floor(( Math.random() * myTips.length ) + 0 );
    window.cpAPIInterface.setVariableValue( 'yourVariable', myTips[ newTip ] );
    }
    </script>

    justinm53961168
    Inspiring
    May 4, 2016

    hey thanks for this - shall I just copy and paste your response as JS?

    I don't know JS so I'm hoping this is all I need:) but would be great if it works!

    TLCMediaDesign
    Inspiring
    May 4, 2016

    You'll need to put it just above the </head> in your html page for it to work.

    Lilybiri
    Legend
    May 4, 2016

    I would use JS for such a request, not really possible with Captivate out of the box. In JS it is easy to create an array with tips, then generate a random number. I am not a JS expert at all, there are better users around, but for randomizing I am always using JS.