Advertisement
trentjs

Show Hide Target Buttons

May 7th, 2021
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.         <style>
  5.  
  6.             body{
  7.                 font-family: Arial, Helvetica, sans-serif;
  8.             }
  9.  
  10.             #section-01, #section-02{
  11.                 border:1px solid #dedede;
  12.                 padding:1em;
  13.             }
  14.  
  15.             #section-01{
  16.                 display: none;
  17.             }
  18.  
  19.         </style>
  20.     </head>
  21. <body>
  22.  
  23. <h1>Show Hide Buttons</h1>
  24.  
  25. <div id="section-01">
  26.     <h3>Div One</h3>
  27.     <button id='nt-show-tabs' type="button" class="nt-show-hide" data-host="section-01" data-target="section-02" data-display="block">Show Tabs</button>
  28. </div>
  29.  
  30. <div id="section-02">
  31.     <h3>The Second Div</h3>
  32.     <button id='nt-hide-tabs' type="button" class="nt-show-hide" data-host="section-02" data-target="section-01" data-display="block">Hide Tabs</button>
  33. </div>
  34.  
  35.  <script>
  36.      
  37.      initShowHideTabsHide();
  38.      function initShowHideTabsHide(){
  39.         var ntShowHideList = document.querySelectorAll(".nt-show-hide");
  40.         for(var i = 0; i < ntShowHideList.length; i++) {
  41.             ntShowHideList[i].addEventListener("click", doShowHideTabsClick);
  42.         }
  43.      }
  44.  
  45.     function doShowHideTabsClick(event){
  46.         var thisId = event.target.id;
  47.         var thisHost = event.target.getAttribute('data-host');
  48.         var thisTargetId = event.target.getAttribute('data-target');
  49.         var thisCssDisplay = event.target.getAttribute('data-display');
  50.  
  51.         var thisHideId = document.getElementById(thisHost);
  52.         var thisShowId = document.getElementById(thisTargetId);
  53.         thisHideId.style.display = "none";
  54.         thisShowId.style.display = thisCssDisplay;
  55.     }
  56.  </script>
  57.  
  58. </body>
  59. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement