Guest User

Untitled

a guest
Jul 4th, 2022
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8.  
  9. <style>
  10. .panel { display: none; }
  11. .show { display: block; }
  12. </style>
  13. </head>
  14. <body>
  15. <div class="buttons">
  16. <button data-id="myorders" class="button">ORDERS</button>
  17. <button data-id="myproducts" class="button">PRODUCTS</button>
  18. <button data-id="mysupplier" class="button">SUPPLIER</button>
  19. </div>
  20.  
  21. <div class="panel" id="myorders"><p>Laptop, Earphone</p></div>
  22. <div class="panel" id="myproducts"><p>Earphone, smart watch</p></div>
  23. <div class="panel" id="mysupplier"><p>Amazon, E-kart</p></div>
  24.  
  25. <script>
  26.  
  27.  
  28.  
  29. // Cache out buttons container, and all of the panels
  30. const buttons = document.querySelector('.buttons');
  31. const panels = document.querySelectorAll('.panel');
  32.  
  33. // Add an event listener to the buttons container
  34. buttons.addEventListener('click', handleClick);
  35.  
  36. // When a child element of `buttons` is clicked
  37. function handleClick(e) {
  38.  
  39. // Check to see if its a button
  40. if (e.target.matches('button')) {
  41.  
  42. // For every element in the `panels` node list use `classList`
  43. // to remove the show class
  44. panels.forEach(panel => panel.classList.remove('show'));
  45.  
  46. // "Destructure" the `id` from the button's data set
  47. const { id } = e.target.dataset;
  48.  
  49. // Create a selector that will match the corresponding
  50. // panel with that id. We're using a template string to
  51. // help form the selector. Basically it says find me an element
  52. // with a "panel" class which also has an id that matches the id of
  53. // the button's data attribute which we just retrieved.
  54. const selector = `.panel[id="${id}"]`;
  55.  
  56. // Select the `div` and, using classList, again add the
  57. // show class
  58. document.querySelector(selector).classList.add('show');
  59. }
  60. }
  61. </script>
  62. </body>
  63. </html>
  64.  
Advertisement
Add Comment
Please, Sign In to add comment