Advertisement
talalaite

Untitled

Mar 18th, 2021 (edited)
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.   </head>
  9.   <body>
  10.     <button>Show BMWs</button>
  11.     <button>Show VWs</button>
  12.     <button>Show ALL</button>
  13.  
  14.     <table>
  15.       <thead>
  16.         <tr>
  17.           <th>Brand</th>
  18.           <th>Model</th>
  19.         </tr>
  20.       </thead>
  21.       <tbody></tbody>
  22.     </table>
  23.  
  24.     <!--Lenteles shortcut:      table>thead>tr>th + tab      -->
  25.  
  26.     <script defer>
  27.       const tbody = document.querySelector("tbody");
  28.       fetch("https://back-end-js-x8whq.ondigitalocean.app/")
  29.         .then((res) => res.json())
  30.         .then((data) =>
  31.           data.forEach((item) => {
  32.             displayTable(data);
  33.           })
  34.         );
  35.  
  36.       // BMW mygtukas
  37.       document.querySelector("button").addEventListener("click", () => {
  38.         fetch("https://back-end-js-x8whq.ondigitalocean.app/bmw")
  39.           .then((res) => res.json())
  40.           .then((data) => displayTable(data));
  41.       });
  42.  
  43.       //   VW mygtukas
  44.       document
  45.         .querySelector("button:nth-child(2)")
  46.         .addEventListener("click", () => {
  47.           fetch("https://back-end-js-x8whq.ondigitalocean.app/vw")
  48.             .then((res) => res.json())
  49.             .then((data) => displayTable(data));
  50.         });
  51.  
  52.       // ALL mygtukas
  53.       document
  54.         .querySelector("button:nth-child(3)")
  55.         .addEventListener("click", () => {
  56.           fetch("https://back-end-js-x8whq.ondigitalocean.app/")
  57.             .then((res) => res.json())
  58.             .then((data) => displayTable(data));
  59.         });
  60.  
  61.       function displayTable(array) {
  62.         tbody.innerHTML = "";
  63.  
  64.         array.forEach((item) => {
  65.           const tr = tbody.insertRow();
  66.  
  67.           const td1 = tr.insertCell();
  68.           td1.textContent = item.brand;
  69.  
  70.           const td2 = tr.insertCell();
  71.           td2.textContent = item.model;
  72.         });
  73.       }
  74.     </script>
  75.   </body>
  76. </html>
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement