Advertisement
ilianrusev

Untitled

Jan 29th, 2022
29
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. window.addEventListener('load', solve);
  2.  
  3. function solve() {
  4. document.getElementById("add").type = "button";
  5. let modelInput = document.getElementById("model")
  6. let yearInput = document.getElementById("year")
  7. let descriptionInput = document.getElementById("description")
  8. let priceInput = document.getElementById("price")
  9. let buttonAdd = document.getElementById("add")
  10. let total = 0;
  11.  
  12. let tBody = document.getElementById("furniture-list")
  13.  
  14. buttonAdd.addEventListener("click", onAdd);
  15.  
  16. function onAdd() {
  17. if (modelInput.value != "" && descriptionInput.value != "" && yearInput.value > 0 && priceInput.value > 0 && yearInput.value != "" && priceInput.value != "") {
  18. let rowInfo = document.createElement("tr")
  19. rowInfo.classList.add("info");
  20. let tdModel = document.createElement("td")
  21. tdModel.textContent = modelInput.value;
  22. let tdPrice = document.createElement("td")
  23. tdPrice.textContent = Number(priceInput.value).toFixed(2);
  24.  
  25. rowInfo.appendChild(tdModel)
  26. rowInfo.appendChild(tdPrice);
  27.  
  28. let td = document.createElement("td")
  29. td.innerHTML = "<button class='moreBtn'>More Info</button> <button class='buyBtn'>Buy it</button>";
  30.  
  31. rowInfo.appendChild(td)
  32.  
  33. let rowHide = document.createElement("tr")
  34. rowHide.classList.add("hide");
  35. let tdYear = document.createElement("td")
  36. tdYear.textContent = `Year: ${yearInput.value}`
  37. let tdDesc = document.createElement("td");
  38. tdDesc.setAttribute("colspan", 3)
  39. tdDesc.textContent = `Description: ${descriptionInput.value}`
  40. rowHide.appendChild(tdYear)
  41. rowHide.appendChild(tdDesc)
  42.  
  43. tBody.appendChild(rowInfo)
  44. tBody.appendChild(rowHide)
  45.  
  46. Array.from(document.querySelectorAll(".moreBtn")).forEach((e) => e.addEventListener("click", onClick))
  47.  
  48. function onClick(ev) {
  49. if (ev.target.textContent == "More Info") {
  50. ev.target.textContent = "Less Info";
  51. ev.target.parentNode.parentNode.parentNode.children[1].style.display = "contents"
  52. } else if (ev.target.textContent == "Less Info") {
  53. ev.target.textContent = "More Info";
  54. ev.target.parentNode.parentNode.parentNode.children[1].style.display = "none"
  55. }
  56.  
  57. }
  58.  
  59. document.querySelector(".buyBtn").addEventListener("click", onBuy);
  60.  
  61. function onBuy(ev) {
  62.  
  63. total += Number(tdPrice.textContent)
  64.  
  65. document.querySelector(".total-price").textContent = total.toFixed(2);
  66.  
  67. ev.target.parentNode.parentNode.parentNode.removeChild(rowHide)
  68. ev.target.parentNode.parentNode.parentNode.removeChild(rowInfo)
  69. }
  70.  
  71. modelInput.value = "";
  72. yearInput.value = "";
  73. descriptionInput.value = "";
  74. priceInput.value = "";
  75.  
  76. }
  77.  
  78. }
  79.  
  80. }
  81.  
Advertisement
RAW Paste Data Copied
Advertisement