audipower

Christmas Gifts Delivery

Dec 10th, 2019
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solution() {
  2.  
  3.     const [addSection, listSection, sentSection, discartedSection] = Array.from(document.getElementsByClassName("card"));
  4.     const addInput = addSection.querySelector("input");
  5.     const btnAdd = addSection.querySelector("button");
  6.  
  7.     btnAdd.addEventListener("click", function () {
  8.         if (addInput.value !== "") {
  9.             let li = document.createElement("li");
  10.             li.classList.add("gift");
  11.             li.textContent = addInput.value;
  12.  
  13.             let btnSend = document.createElement("button");
  14.             btnSend.id = "sendButton";
  15.             btnSend.textContent = "Send";
  16.  
  17.             let btnDiscard = document.createElement("button");
  18.             btnDiscard.id = "discardButton";
  19.             btnDiscard.textContent = "Discard";
  20.  
  21.             li.appendChild(btnSend);
  22.             li.appendChild(btnDiscard);
  23.  
  24.             listSection.querySelector("ul").appendChild(li);
  25. //-------------------------------------------------------------------------------------------------------
  26.             let allListItems = Array.from(listSection.querySelectorAll("ul li")).sort((a, b) => a.textContent.localeCompare(b.textContent))
  27.  
  28.             listSection.querySelector("ul").innerHTML = "";
  29.  
  30.             allListItems.forEach(element => listSection.querySelector("ul").appendChild(element));
  31. //---------------------------------------------------------------------------------------------------------
  32.             addInput.value = "";
  33.  
  34.             btnSend.addEventListener("click", function (e) {
  35.                 let giftSend = e.target.parentNode.firstChild.textContent;
  36.                 let liSend = document.createElement("li");
  37.                 liSend.textContent = giftSend;
  38.                 sentSection.querySelector("ul").appendChild(liSend);
  39.                 e.target.parentNode.remove();
  40.             });
  41.  
  42.             btnDiscard.addEventListener("click", function (e) {
  43.                 let giftSend = e.target.parentNode.firstChild.textContent;
  44.                 let liSend = document.createElement("li");
  45.                 liSend.textContent = giftSend;
  46.                 discartedSection.querySelector("ul").appendChild(liSend);
  47.                 e.target.parentNode.remove();
  48.             });
  49.         }
  50.     });
  51. }
Add Comment
Please, Sign In to add comment