Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solution() {
- let type = document.getElementById("toyType");
- let price = document.getElementById("toyPrice");
- let description = document.getElementById("toyDescription");
- let addBtn = document.getElementsByTagName("button")[0];
- let sectionGift = document.getElementById("christmasGiftShop");
- addBtn.addEventListener("click", function (e) {
- e.preventDefault();
- if (
- type.value === "" ||
- price.value === "" ||
- Number.isNaN(Number(price.value)) ||
- description.value === "" ||
- description.value.length < 50
- ) {
- return;
- } else {
- let div = ce("div", "", "gift");
- let img = ce("img");
- img.src = "gift.png";
- let h2 = ce("h2", type.value);
- let p = ce("p", description.value);
- let buyBtn = ce("button", `Buy it for $${price.value}`);
- div.appendChild(img);
- div.appendChild(h2);
- div.appendChild(p);
- div.appendChild(buyBtn);
- type.value = "";
- price.value = "";
- description.value = "";
- sectionGift.appendChild(div);
- buyBtn.addEventListener("click", function (e) {
- e.target.parentNode.remove();
- });
- }
- });
- function ce(el, text, className, id) {
- let e = document.createElement(el);
- if (text) {
- e.textContent = text;
- }
- if (className) {
- e.classList = className;
- }
- if (id) {
- e.id = id;
- }
- return e;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment