Liliana797979

1_furniture store - js advanced exam

Oct 15th, 2021
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     const modelField = document.getElementById("model");
  3.     const yearField = document.getElementById("year");
  4.     const descriptionField = document.getElementById("description");
  5.     const priceField = document.getElementById("price");
  6.  
  7.     const addButton = document.getElementById("add");
  8.     addButton.addEventListener("click", addFurniture);
  9.  
  10.     const furnitureList = document.getElementById("furniture-list");
  11.     const totalPrice = document.querySelector(".total-price");
  12.  
  13.  
  14.  
  15.     function addFurniture(e) {
  16.         e.preventDefault();
  17.  
  18.         const yearValue = Number(yearField.value);
  19.         const priceValue = Number(priceField.value);
  20.         if (modelField.value != "" && descriptionField.value != "" && yearValue > 0 && priceValue > 0) {
  21.             const tr = document.createElement("tr");
  22.             tr.classList.add("info");
  23.             tr.innerHTML = `<td>${modelField.value}</td>
  24.                             <td>${priceValue.toFixed(2)}</td>
  25.                             <td><button class="moreBtn">More Info</button>
  26.                                 <button class="buyBtn">Buy it</button>
  27.                             </td>`;
  28.             const hideTr = document.createElement("tr");
  29.             hideTr.classList.add("hide");
  30.             hideTr.innerHTML = `<td>Year: ${yearValue}</td><td colspan="3">Description: ${descriptionField.value}</td>`
  31.  
  32.             furnitureList.appendChild(tr);
  33.             furnitureList.appendChild(hideTr);
  34.  
  35.             const moreInfoButtons = tr.querySelectorAll("button");
  36.             moreInfoButtons[0].addEventListener("click", showMoreInfo);
  37.             moreInfoButtons[1].addEventListener("click", buyFurniture);
  38.  
  39.         }
  40.  
  41.         modelField.value = "";
  42.         yearField.value = "";
  43.         descriptionField.value = "";
  44.         priceField.value = "";
  45.     }
  46.  
  47.     function showMoreInfo(e) {
  48.         const moreInfoTr = e.target.parentElement.parentElement.nextElementSibling;
  49.         if (e.target.textContent == "More Info") {
  50.             e.target.textContent = "Less Info";
  51.             moreInfoTr.style.display = "contents";
  52.         } else {
  53.             e.target.textContent = "More Info";
  54.             moreInfoTr.style.display = "none";
  55.         }
  56.     }
  57.  
  58.     function buyFurniture(e) {
  59.         const tr = e.target.parentElement.parentElement;
  60.         const hideTr = tr.nextElementSibling;
  61.  
  62.         hideTr.parentElement.removeChild(hideTr);
  63.  
  64.         const price = Number(tr.querySelectorAll("td")[1].textContent);
  65.         totalPrice.textContent = (Number(totalPrice.textContent) + price).toFixed(2);
  66.  
  67.         tr.parentElement.removeChild(tr);
  68.     }
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment