Can't Hide a Div on (document).ready

Can't Hide a Div on (document).ready

Hello, I'm using the Tableau JavaScript API to toggle between two dashboards. The two buttons on the page I have work fine to do this, as they are just showing and hiding some divs that the dashboards are in. The problem I'm having is hiding one of the dashboards when the page loads. I have a feeling its a timing issue, but not sure what the problem is, Help appreciated!

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  5. <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  6. <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  7. <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">

  8. <style>
  9. .btn {
  10.   border: 2px solid black;
  11.   background-color: #336699;
  12.   color: black;
  13.   padding: 10px 16px;
  14.   font-size: 15px;
  15.   cursor: pointer;
  16.   font-family: Arial, Helvetica, sans-serif;
  17. text-decoration: none;
  18. }

  19. /* Blue */
  20. .launch {
  21.   border-color: #2196F3;
  22.   color: #ecf2f9;
  23. }

  24. .launch:hover {
  25.   background: #2196F3;
  26.   color: white;
  27. }

  28. .launch:focus {
  29.   background: #2196F3;
  30.   color: white;
  31. }

  32. .default:hover {
  33.   background: #e7e7e7;
  34. }
  35. </style>

  36. <script>
  37. $(document).ready(function() {
  38.     $("#viz1558393573094").hide();
  39. });
  40. </script>

  41. </head>

  42. <body>


  43. <div style="overflow:auto" id='buttons'>
  44. <button id="btn1" class="btn launch">Red/Green</button>
  45. <button id="btn2" class="btn launch">Blue/Gold</button>
  46. </div>

  47. <div class='tableauPlaceholder' id='viz1558393573094' style='position: relative'>
  48.   <noscript><a href='#'><img alt=' '    

  49. src='https:&#47;&#47;public.tableau.com&#47;static&#47;images&#47;Su&#47;SuperStoreProfit_15582820781270&#47;Profit&#47;1_rss.png' style='border:         

  50. none' /></a>
  51.   </noscript>

  52.   <object class='tableauViz'  style='display:none;'><param name='host_url' value='https%3A%2F%2Fpublic.tableau.com%2F' /> <param name='embed_code_version'    

  53.   value='3' /> <param name='site_root' value='' /><param name='name' value='SuperStoreProfit_15582820781270&#47;Profit' /><param name='tabs' value='no'      

  54. /><param name='toolbar' value='no' /><param name='static_image'      

  55. value='https:&#47;&#47;public.tableau.com&#47;static&#47;images&#47;Su&#47;SuperStoreProfit_15582820781270&#47;Profit&#47;1.png' /> <param      

  56. name='animate_transition' value='yes' /><param name='display_static_image' value='yes' /><param name='display_spinner' value='yes' /><param      

  57. name='display_overlay' value='yes' /><param name='display_count' value='yes' />
  58.   </object>
  59. </div> 

  60. <div class='tableauPlaceholder' id='viz1558381354848' style='position: relative'>
  61. <noscript><a href='#'><img alt=' ' 

  62. src='https:&#47;&#47;public.tableau.com&#47;static&#47;images&#47;Su&#47;SuperStoreProfit_15582820781270&#47;Profit2&#47;1_rss.png' style='border: none' 

  63. /></a></noscript>
  64. <object class='tableauViz'  style='display:none;'><param name='host_url' value='https%3A%2F%2Fpublic.tableau.com%2F' /> <param name='embed_code_version' 

  65. value='3' /> <param name='site_root' value='' /><param name='name' value='SuperStoreProfit_15582820781270&#47;Profit2' /><param name='tabs' value='no' 

  66. /><param name='toolbar' value='no' /><param name='static_image' 

  67. value='https:&#47;&#47;public.tableau.com&#47;static&#47;images&#47;Su&#47;SuperStoreProfit_15582820781270&#47;Profit2&#47;1.png' /> <param 

  68. name='animate_transition' value='yes' /><param name='display_static_image' value='yes' /><param name='display_spinner' value='yes' /><param 

  69. name='display_overlay' value='yes' /><param name='display_count' value='yes' />
  70. </object>
  71. </div>  
  72.        
  73. <script type='text/javascript'>
  74.      var divElement = document.getElementById('viz1558381354848');
  75.      var vizElement = divElement.getElementsByTagName('object')[0];                    if ( divElement.offsetWidth > 800 ) { 

  76. vizElement.style.width='1000px';vizElement.style.height='800px';} else if ( divElement.offsetWidth > 500 ) { 

  77. vizElement.style.width='100%';vizElement.style.height=(divElement.offsetWidth*0.75)+'px';} else { 

  78. vizElement.style.width='100%';vizElement.style.height='1500px';}                     var scriptElement = document.createElement('script');                    

  79. scriptElement.src = 'https://public.tableau.com/javascripts/api/viz_v1.js';                    vizElement.parentNode.insertBefore(scriptElement, vizElement);
  80.           
  81. </script>

  82. <script type='text/javascript'> 
  83.         var divElement2 = document.getElementById('viz1558393573094');
  84.         var vizElement2 = divElement2.getElementsByTagName('object')[0];
  85.                     if ( divElement2.offsetWidth > 800 ) { vizElement2.style.width='1000px';vizElement2.style.height='800px';} 
  86.                     else if ( divElement2.offsetWidth > 500 ) { vizElement2.style.width='100%';vizElement2.style.height=(divElement2.offsetWidth*0.75)+'px';} 
  87.                     else { vizElement2.style.width='100%';vizElement2.style.height='1500px';} 
  88.                     var scriptElement2 = document.createElement('script');
  89.                     scriptElement2.src = 'https://public.tableau.com/javascripts/api/viz_v1.js';
  90.                     vizElement2.parentNode.insertBefore(scriptElement2, vizElement2); 
  91.  </script>


  92. <script type="text/javascript">
  93.         document.getElementById("btn1").addEventListener("click", function(){
  94.     //alert("oh snap, i was clicked...");

  95.   $('#viz1558381354848').hide(); 
  96.   $('#viz1558393573094').show();  

  97.   });
  98.     </script>

  99. <script type="text/javascript">
  100.         document.getElementById("btn2").addEventListener("click", function(){
  101.     //alert("oh snap, i was clicked...");
  102.   
  103.         $('#viz1558393573094').hide();
  104.         $('#viz1558381354848').show();
  105.        
  106.   });
  107.  </script>

  108. </body>

  109. <html>