krasizorbov

Pet me

Oct 4th, 2020
946
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.   let input = document.querySelectorAll("input[type='text']");
  3.   let addBtn = document.querySelector("button");
  4.   let section = document.querySelectorAll("section");
  5.   let adoption = section[0];
  6.   let ulAdoption = adoption.children[1];
  7.   let adopted = section[1];
  8.   let ulAdopted = adopted.children[1];
  9.  
  10.   let name = input[0];
  11.   let age = input[1];
  12.   let kind = input[2];
  13.   let owner = input[3];
  14.  
  15.   addBtn.addEventListener("click", function (e) {
  16.     e.preventDefault();
  17.     if (
  18.       name.value === "" ||
  19.       age.value === "" ||
  20.       kind.value === "" ||
  21.       owner.value === "" ||
  22.       Number.isNaN(Number(age.value))
  23.     ) {
  24.       return;
  25.     }
  26.     let li = document.createElement("li");
  27.     let p = document.createElement("p");
  28.     let strong1 = ce("strong", name.value);
  29.     let strong2 = ce("strong", age.value);
  30.     let strong3 = ce("strong", kind.value);
  31.     let span = ce("span", `Owner: ${owner.value}`);
  32.     let button = ce("button", "Contact with owner");
  33.  
  34.     p.appendChild(strong1);
  35.     p.innerHTML += " is a ";
  36.     p.appendChild(strong2);
  37.     p.innerHTML += " year old ";
  38.     p.appendChild(strong3);
  39.     li.appendChild(p);
  40.     li.appendChild(span);
  41.     li.appendChild(button);
  42.     ulAdoption.appendChild(li);
  43.  
  44.     name.value = "";
  45.     age.value = "";
  46.     kind.value = "";
  47.     owner.value = "";
  48.  
  49.     button.addEventListener("click", function (e) {
  50.       let div = document.createElement("div");
  51.       let input = document.createElement("input");
  52.       input.placeholder = "Enter your names";
  53.       let changeBtn = ce("button", "Yes! I take it!");
  54.  
  55.       div.appendChild(input);
  56.       div.appendChild(changeBtn);
  57.       li.appendChild(div);
  58.       e.target.remove();
  59.  
  60.       changeBtn.addEventListener("click", function (e) {
  61.         if (input.value !== "") {
  62.           let newspan = ce("span", `New Owner: ${input.value}`);
  63.           let checkedBtn = ce("button", "Checked");
  64.           let parentDiv = e.target.parentNode;
  65.           let parent = parentDiv.parentNode;
  66.           parentDiv.removeChild(input);
  67.           parentDiv.removeChild(changeBtn);
  68.           parent.removeChild(parentDiv);
  69.           parent.removeChild(span);
  70.           parent.appendChild(newspan);
  71.           parent.appendChild(checkedBtn);
  72.           ulAdopted.appendChild(parent);
  73.           e.target.remove();
  74.  
  75.           checkedBtn.addEventListener("click", function (e) {
  76.             parent.remove();
  77.           });
  78.         }
  79.       });
  80.     });
  81.   });
  82.  
  83.   function ce(el, text, className, id) {
  84.     let e = document.createElement(el);
  85.     if (text) {
  86.       e.textContent = text;
  87.     }
  88.     if (className) {
  89.       e.classList = className;
  90.     }
  91.     if (id) {
  92.       e.id = id;
  93.     }
  94.     return e;
  95.   }
  96. }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment