Copy link to clipboard
Copied
I'm trying to write a function to save text entry box values to local storage.
I wrote my code and saved it in course files, to make it run I need to execute 'makehtmlsave();' on every slide load.
I tried using a listener on 'CPAPI_SLIDEENTER' to execute the code, but somehow this won't work (I thought maybe because my script uses jquery)
I thought I could include the function makehtmlsave(); as part of advanced actions I have running on slide entry, but no luck.
If I create a simple button though just the function makehtmlsave(); this works just fine?
Why would it run in the button simple action but not as part of an advanced action?
Copy link to clipboard
Copied
This is the whole code I a trying to run incase it helps. I am trying to make text entry boxes re-populate with their previous values is a user leaves and comes back.
function makehtmlsave(){
$("textarea").keyup(function () {
localStorage[$(this).attr('id')] = $(this).val();
});
$("textarea").each(function (i){
$("#" + this.id).val(localStorage[this.id]);
});
}
Copy link to clipboard
Copied
Just realised it willl work on a button click (the advanced action) so why won't it run on slide entry!? argh!
Copy link to clipboard
Copied
Binding events to Captivate objects is iffy. Also, even if the code worked you would be writing to localStorage after every character typed.
A TEB is made from several elements, for example:
Text_Entry_Box_1
Text_Entry_Box_1c
Text_Entry_Box_1_inputField
Text_Entry_Box_1 (variable name)
To make a TEB repopulate you need to set the TEB to retain value and manipulate the TEB's variable. You could use the TEB's on success to write to the localStorage.
To repopulate the variable I would call the read localStorage function on enter of the first slide and repopulate the TEB's variable.
Copy link to clipboard
Copied
Are you adding your listener to the cpAPIEventEmitter object? Your listener should look something like this:
window.cpAPIEventEmitter.addEventListener("CPAPI_SLIDEENTER",makehtmlsave);
Copy link to clipboard
Copied
If you're wanting to just save a TEB's text to local storage, there's a much easier way to do that now.
With the CpExtra widget you can create a Local Storage Variable whose value is automatically stored and recalled from local storage.
You can find more information about CpExtra's local storage variables here: Persisting Data with CpExtra | Infosemantics Pty Ltd‌
You might also want to look into these settings which stop Text Entry Boxes overwriting their linked variable values with their default text: Text Entry Box Behavior | Infosemantics Pty Ltd
Tristan,