Advertisement
divanov94

Untitled

Dec 8th, 2020
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.  
  3.     const divElem = document.querySelector('#container');
  4.     let [name, hall, ticketPrice, addBtn] = Array.from(divElem.children);
  5.     let movieSection = document.querySelector('#movies>ul');
  6.     let archiveSection = document.querySelector('#archive>ul');
  7.     let clearBtn = document.querySelector('#archive>button');
  8.  
  9.     addBtn.addEventListener('click', addMovie);
  10.  
  11.     function addMovie(e) {
  12.         e.preventDefault();
  13.  
  14.         let nameVal = name.value;
  15.         let hallVal = hall.value;
  16.         let ticketVal = Number(ticketPrice.value);
  17.         let archiveBtn = el('button', 'Archive')
  18.         let input = el('input', '', { placeholder: 'Tickets Sold' })
  19.  
  20.         if (nameVal && hallVal && ticketVal) {
  21.             if (!Number(ticketVal)) {
  22.                 return;
  23.             }
  24.             let movie = el('li', [
  25.                 el('span', nameVal),
  26.                 el('strong', `Hall: ${hallVal}`),
  27.                 el('div', [
  28.                     el('strong', `${ticketVal.toFixed(2)}`),
  29.                     input,
  30.                     archiveBtn
  31.                 ])
  32.             ]);
  33.  
  34.             movieSection.appendChild(movie)
  35.  
  36.             archiveBtn.addEventListener('click', () => {
  37.  
  38.                 if (!Number(input.value) || input.value == '') {
  39.                     return;
  40.                 }
  41.                 let delBtn = el('button', 'Delete')
  42.  
  43.  
  44.                 let archiveMovie = el('li', [
  45.                     el('span', nameVal),
  46.                     el('strong', `Total amount: ${(ticketVal * input.value).toFixed(2)}`),
  47.                     delBtn
  48.                 ])
  49.                 movie.remove();
  50.  
  51.  
  52.                 archiveSection.appendChild(archiveMovie);
  53.  
  54.                 delBtn.addEventListener('click', () => {
  55.                     archiveMovie.remove();
  56.                 });
  57.  
  58.                 clearBtn.addEventListener('click', () => {
  59.                     archiveMovie.parentElement.remove()
  60.                 })
  61.             })
  62.            
  63.         }
  64.     }
  65.  
  66.  
  67.  
  68.     function el(type, content, attributes) {
  69.         const result = document.createElement(type);
  70.  
  71.         if (attributes !== undefined) {
  72.             Object.assign(result, attributes);
  73.         }
  74.  
  75.         if (Array.isArray(content)) {
  76.             content.forEach(append);
  77.         } else {
  78.             append(content);
  79.         }
  80.  
  81.         function append(node) {
  82.             if (typeof node === 'string' || typeof node === 'number') {
  83.                 node = document.createTextNode(node);
  84.             }
  85.             result.appendChild(node);
  86.         }
  87.         return result;
  88.     }
  89.    
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement