Skip to main content
Participating Frequently
December 12, 2022
Answered

How to get/set Javascript Variables in different Slides ?

  • December 12, 2022
  • 1 reply
  • 603 views

Hey,

 

in the first Slide of my Projekt I´m executing a Javascript code in the Script Window to generate random questions that are stored in an array and then go to the next slide. In the next slide I want to use the same array to generate a new random question and delete the previous one so that it can appear only one time.

 

How can I get and set javascript Variables - that are difined in the Script Window on a slide - from other sliders ?

any help would be appreciated ^^

 

 

This topic has been closed for replies.
Correct answer TLCMediaDesign

You need to create and initialize the variable in Captivates variables window. Then set the variable like this:

 

window.randQuestions_List = [ "What is one important skill that you think everyone should have?",
"There are now 25 hours in a day! How do you spend your extra hour?",
"What was the most fun thing you did last weekend?",
"What is one thing you are looking forward to doing when you retire?",
"What is one of your greatest achievements?",
"If you could change one thing about your job, what would it be?"
];

1 reply

Lilybiri
Legend
December 12, 2022

Not sure to understand your problem, which may be due to a language problem. You can write in your language, translation is possible in this forum. Personally I do understand a couple of languages besides English (not my mother tongue).

I suspect you want to override the default setup for random questions, to avoid the need to restart a session to get another batch of random questions. Is that correct?

You didn't post the JS code you are using, you added the tags Advanced actions: how do you use this, which type of items do you have in the array (you cannot have complete slides AFAIK)?  Do you transfer the value for variables generated by JS to Captivate variables? Just some questions which may help you to clarify your request.

Here is a recent post I wrote about randomization in Captivate:

https://blog.lilybiri.com/randomizing-in-captivate

This older post talks about the possibilities and limitations of random questions:

https://blog.lilybiri.com/random-questions-dos-and-donts

 

Participating Frequently
December 12, 2022

this is the code that I´m using to generate the random question:

var randQuestions_List = [ "What is one important skill that you think everyone should have?",
"There are now 25 hours in a day! How do you spend your extra hour?",
"What was the most fun thing you did last weekend?",
"What is one thing you are looking forward to doing when you retire?",
"What is one of your greatest achievements?",
"If you could change one thing about your job, what would it be?"
];
var randQuestion = randQuestions_List[Math.floor(Math.random()*(randQuestions_List).length)];
window.cpAPIInterface.setVariableValue('hzdRandQuestion', randQuestion);
//remove Item from the Array
const index = array.indexOf(randQuestion);
if (index > -1) { // only splice array when item is found
  randQuestions_List.splice(index, 1); // 2nd parameter means remove one item only
}
//go to the next slide
window.cpAPIInterface.setVariableValue('cpCmndGotoSlideAndResume', 2);

 

In the next Slide I want to use the same Array but the previous Question should be deleted so that it does not appeare another time. the Goal is to generate random Quations in each slide without repeating the question.

The Problem is that i cant get to the same array and I have to redifine it to use it in the script window.

thank you for your help ^^

TLCMediaDesign
TLCMediaDesignCorrect answer
Inspiring
December 12, 2022

You need to create and initialize the variable in Captivates variables window. Then set the variable like this:

 

window.randQuestions_List = [ "What is one important skill that you think everyone should have?",
"There are now 25 hours in a day! How do you spend your extra hour?",
"What was the most fun thing you did last weekend?",
"What is one thing you are looking forward to doing when you retire?",
"What is one of your greatest achievements?",
"If you could change one thing about your job, what would it be?"
];