Skip to main content
Inspiring
May 23, 2022
Answered

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

  • May 23, 2022
  • 1 reply
  • 1026 views

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.

    This topic has been closed for replies.
    Correct answer Stagprime2687219

    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.

    1 reply

    Stagprime2687219
    Stagprime2687219Correct answer
    Legend
    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.

    AlmirRVSAuthor
    Inspiring
    May 23, 2022

    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);
    AlmirRVSAuthor
    Inspiring
    May 24, 2022

    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.


    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.