• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to get/set Javascript Variables in different Slides ?

Explorer ,
Dec 11, 2022 Dec 11, 2022

Copy link to clipboard

Copied

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 ^^

 

 

TOPICS
Advanced , Advanced actions

Views

218

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

People's Champ , Dec 12, 2022 Dec 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, w
...

Votes

Translate

Translate
Community Expert ,
Dec 12, 2022 Dec 12, 2022

Copy link to clipboard

Copied

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

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 12, 2022 Dec 12, 2022

Copy link to clipboard

Copied

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 ^^

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Dec 12, 2022 Dec 12, 2022

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 12, 2022 Dec 12, 2022

Copy link to clipboard

Copied

I will leave the JS part to our great guru, TLCMediaDesign.

With the Captivate part I can offer some tips:

  • You cannot insert that script with its comments in the Captivate Script window which you start with 'Execute Javascript'. How did you fix that? 
  • I would not use JS for Go to the Next slide, any reason why this is done?
  • You need to insert the Captivate variable (see TLCMediaDesign) in your question slide to have the question appear. But how do you check if the question is answered correctly? Or do you insert the variable in a default Quiz slide of the type 'True/False' to take care of it?

You didn't answer all my questions in my previous content, only the one asking for the script.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 13, 2022 Dec 13, 2022

Copy link to clipboard

Copied

Hey,

  • Exactly I deleted the comments and its working.
  • I changed that too and just used the advanced action.
  • In my Project I don't check whether the answer is right or not. I just registered in a Captivate Variable and display it at the end of the cours.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 13, 2022 Dec 13, 2022

Copy link to clipboard

Copied

LATEST

OK, but since you use an advanced action, why do you not add the simple command 'Go to Next Slide' instead of using the system variable in the JS window.

You are creating a sort of Survey, but do not want to have it reported at all. Showing the result of the questions on a summary slide is only meant for the learner. If you want to report to a LMS your approach wouldn't work. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Help resources