Skip to main content
Inspiring
May 23, 2022
Answered

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

  • May 23, 2022
  • 7 replies
  • 1063 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 is closed to new replies. Start a new post to keep the conversation going.
    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.

    7 replies

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