Copy link to clipboard
Copied
Is it possible to check if a specific object is hidden or visible with JavaScript?
I know how to show or hide an object with JavaScript but I need to verify if an object is hidden or visible using JavaScript.
You can do the following...
cp.D.myObjectNamec.visible
In the browser console this will return a 1 if the object is visible and a 0 if the object is hidden.
Simply replace the myObjectName in the code above with the name of the object you wish to query.
Do not remove the c at the end.
You could also assign the above code to a variable and apply this to a button and then provide a smartShape to display the value of the variable and then do stuff based on that value.
Hope that helps.
Copy link to clipboard
Copied
You can do the following...
cp.D.myObjectNamec.visible
In the browser console this will return a 1 if the object is visible and a 0 if the object is hidden.
Simply replace the myObjectName in the code above with the name of the object you wish to query.
Do not remove the c at the end.
You could also assign the above code to a variable and apply this to a button and then provide a smartShape to display the value of the variable and then do stuff based on that value.
Hope that helps.
Copy link to clipboard
Copied
Thanks a lot! That solved my problem.
As I needed the object name to be variable I did that using eval(). See example bellow, just for the records and if someone eventually need it:
var nS = cpInfoCurrentSlideLabel;
var txtB = "cp.D." + nS + "_button_namec.visible";
var txtM = "cp.D." + nS + "_msgc.visible";
var vButton = eval(txtB);
var vMessage = eval(txtM);
alert("Button visibility: " + vButton + " | Message visibility: " + vMessage);
Copy link to clipboard
Copied
I would add that somtimes Captivate will return true or false instead of 1 or 0, so you would need to chack for that.
It is really bad form to use eval, there are many ways to get around uing that method. window [] syntax is much better.
In your code above, the slide label is in a variable, but not the button name.
Copy link to clipboard
Copied
Thank you for your answer!
If you don't mind, how would I use the window [] syntax to avoid using eval?
Yes, I know the button name is not in a variable, that code is just an example that I used to test the visible property.
Copy link to clipboard
Copied
If you could post your actual code, I could show you.
Copy link to clipboard
Copied
Hi, this is part of the actual code (On Enter Slide event):
//-- On Enter Slide event
var nS = cpInfoCurrentSlideLabel; // "Q01";
var nomeBotao = "_botao_responder"; // button name
var txtB = "cp.D." + nS + nomeBotao + "c.visible";
var vB = eval(txtB);
if (vB === 1) {
cp.hide(nS + "_next");
cp.hide(nS + "_next_img");
}
else {
cp.show(nS + "_next");
cp.show(nS + "_next_img");
}
Copy link to clipboard
Copied
I added the code if it returns true instead of 1.
The line in bold is where the brackets are used.
var nS = cpInfoCurrentSlideLabel; // "Q01";
var nomeBotao = "_botao_responder"; // button name
var vB = cp.D[nS + nomeBotao + "c"].visible;
if ( vB === 1 || vB === true ) {
cp.hide(nS + "_next");
cp.hide(nS + "_next_img");
}
else {
cp.show(nS + "_next");
cp.show(nS + "_next_img");
}