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

How do you know if an object is hidden with JavaScript?

Explorer ,
May 23, 2022 May 23, 2022

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.

Views

415

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

Advisor , May 23, 2022 May 23, 2022

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.

Votes

Translate

Translate
Advisor ,
May 23, 2022 May 23, 2022

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.

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 ,
May 23, 2022 May 23, 2022

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);

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 ,
May 24, 2022 May 24, 2022

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.

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 ,
May 24, 2022 May 24, 2022

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.

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 ,
May 24, 2022 May 24, 2022

Copy link to clipboard

Copied

If you could post your actual code, I could show you.

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 ,
May 24, 2022 May 24, 2022

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");
}

 

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 ,
May 24, 2022 May 24, 2022

Copy link to clipboard

Copied

LATEST

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");
}

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