Liliana797979

central cinema - js advanced

Oct 1st, 2021
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     const [name, hall, ticketPrice] = document.querySelectorAll("#container input");
  3.     const movieSection = document.querySelector("#movies ul");
  4.     const archiveSection = document.querySelector("#archive ul");
  5.     const clearButton = archiveSection.parentElement.querySelector("button");
  6.     clearButton.addEventListener("click", ()=> {
  7.         archiveSection.innerHTML = "";
  8.     })
  9.     const addMovieButton = document.querySelector("#container button");
  10.     addMovieButton.addEventListener("click", addMovie);
  11.  
  12.  
  13.  
  14.     function addMovie(e) {
  15.         e.preventDefault();
  16.         if (name.value !== "" && hall.value !== "" && ticketPrice.value != "" && !isNaN(Number(ticketPrice.value))) {
  17.             const movie = document.createElement("li");
  18.             movie.innerHTML =
  19.                 `<span>${name.value}</span>
  20.                  <strong>${hall.value}</strong>
  21.                 <div>
  22.                     <strong>${Number(ticketPrice.value).toFixed(2)}</strong>
  23.                     <input placeholder="Tickets Sold">
  24.                     <button >Archive</button>
  25.                 </div>`
  26.             movieSection.appendChild(movie);
  27.  
  28.             const button = movie.querySelector("div button");
  29.             button.addEventListener("click", addToArchive);
  30.             name.value = "";
  31.             hall.value = "";
  32.             ticketPrice.value = "";
  33.         }
  34.     }
  35.  
  36.  
  37.     function addToArchive(e) {
  38.         const inputValue = e.target.parentElement.querySelector("input");
  39.         const ticketPrice = e.target.parentElement.querySelector("strong");
  40.         const movieName = e.target.parentElement.parentElement.querySelector("span");
  41.         if (inputValue.value != "" && !isNaN(Number(inputValue.value))) {
  42.             const income = Number(inputValue.value) * Number(ticketPrice.textContent);
  43.  
  44.             const liEl = document.createElement("li");
  45.             liEl.innerHTML = `<span>${movieName.textContent}</span>
  46.                               <strong>Total amount: ${income.toFixed(2)}</strong>
  47.                               <button>Delete</button>`
  48.  
  49.             const button = liEl.querySelector("button");
  50.             button.addEventListener("click", deleteEntry);
  51.             archiveSection.appendChild(liEl);
  52.         }
  53.  
  54.         e.target.parentElement.parentElement.remove();
  55.     }
  56.  
  57.     function deleteEntry(e) {
  58.         e.target.parentElement.remove();
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment