krasizorbov

X-mas Gifts

Oct 6th, 2020
848
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solution() {
  2.   let type = document.getElementById("toyType");
  3.   let price = document.getElementById("toyPrice");
  4.   let description = document.getElementById("toyDescription");
  5.   let addBtn = document.getElementsByTagName("button")[0];
  6.   let sectionGift = document.getElementById("christmasGiftShop");
  7.  
  8.   addBtn.addEventListener("click", function (e) {
  9.     e.preventDefault();
  10.  
  11.     if (
  12.       type.value === "" ||
  13.       price.value === "" ||
  14.       Number.isNaN(Number(price.value)) ||
  15.       description.value === "" ||
  16.       description.value.length < 50
  17.     ) {
  18.       return;
  19.     } else {
  20.       let div = ce("div", "", "gift");
  21.       let img = ce("img");
  22.       img.src = "gift.png";
  23.       let h2 = ce("h2", type.value);
  24.       let p = ce("p", description.value);
  25.       let buyBtn = ce("button", `Buy it for $${price.value}`);
  26.  
  27.       div.appendChild(img);
  28.       div.appendChild(h2);
  29.       div.appendChild(p);
  30.       div.appendChild(buyBtn);
  31.  
  32.       type.value = "";
  33.       price.value = "";
  34.       description.value = "";
  35.  
  36.       sectionGift.appendChild(div);
  37.  
  38.       buyBtn.addEventListener("click", function (e) {
  39.         e.target.parentNode.remove();
  40.       });
  41.     }
  42.   });
  43.  
  44.   function ce(el, text, className, id) {
  45.     let e = document.createElement(el);
  46.     if (text) {
  47.       e.textContent = text;
  48.     }
  49.     if (className) {
  50.       e.classList = className;
  51.     }
  52.     if (id) {
  53.       e.id = id;
  54.     }
  55.     return e;
  56.   }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment