Advertisement
milovan7

Central Cinema / JS Advanced

Apr 26th, 2021
714
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. function solve() {
  2. let displayMovie = document.querySelector('#movies ul');
  3. let archive = document.querySelector('#archive ul');
  4. archive.nextElementSibling.addEventListener('click', () => archive.innerHTML = null); // clear archive
  5. document.querySelector('button').addEventListener('click', addMovieOnScreen);
  6.  
  7. function addMovieOnScreen(e) {
  8. e.preventDefault();
  9. let [name, hall, price] = document.querySelectorAll('#container input');
  10.  
  11. if (name.value && hall.value && Number(price.value) || price.value === '0') {
  12.  
  13. displayMovie.innerHTML +=
  14. `<li>
  15. <span>${name.value}</span>
  16. <strong>Hall: ${hall.value}</strong>
  17. <div>
  18. <strong>${price.value}</strong>
  19. <input placeholder="Tickets Sold">
  20. <button>Archive</button>
  21. </div>
  22. </li>`
  23.  
  24. name.value = null;
  25. hall.value = null;
  26. price.value = null;
  27.  
  28. }
  29.  
  30. Array.from(displayMovie.querySelectorAll('button')).forEach(btn => btn.addEventListener('click', addToArchive));
  31.  
  32. function addToArchive(btn) {
  33.  
  34. let ticketPrice = btn.target.parentNode.children[0];
  35. let ticketsSold = btn.target.parentNode.children[1];
  36.  
  37. if (Number(ticketsSold.value) || ticketsSold.value === '0') {
  38.  
  39. archive.innerHTML +=
  40. `<li>
  41. <span>${btn.target.parentNode.parentNode.children[0].textContent}</span>
  42. <strong>Total amount: ${(ticketPrice.textContent * ticketsSold.value).toFixed(2)}</strong>
  43. <button>Delete</button>
  44. </li>`
  45.  
  46. btn.target.parentNode.parentNode.remove(); // remove movie from screen
  47. Array.from(archive.querySelectorAll('button')).forEach(btn => btn.addEventListener('click', (btn) => btn.target.parentNode.remove())); // remove movie from archive
  48.  
  49. }
  50. }
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement