Advertisement
GeorgiLukanov87

01. Music Site - JS Advanced Final Exam - 23 October 2021

Mar 23rd, 2023 (edited)
899
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // 01. Music Site
  2. // JS Advanced Final Exam - 23 October 2021
  3. // https://judge.softuni.org/Contests/Practice/Index/3235#0
  4.  
  5. function solve() {
  6.     // Get the form
  7.     const form = document.querySelector('form');
  8.     // Get input fields
  9.     const genreInput = document.querySelector('#genre');
  10.     const nameInput = document.querySelector('#name');
  11.     const authorInput = document.querySelector('#author');
  12.     const dateInput = document.querySelector('#date');
  13.     // Get likes paragraph
  14.     const likes = document.querySelector('#total-likes .likes p')
  15.     var totalLikes = 0;
  16.  
  17.     // Listen for the form submit event
  18.     form.addEventListener('submit', function (event) {
  19.         if (!(genreInput.value === '' ||
  20.             nameInput.value === '' ||
  21.             authorInput.value === '' ||
  22.             dateInput.value === '')) {
  23.  
  24.             event.preventDefault();
  25.             const allHitsContainer = document.getElementsByClassName('all-hits-container')[0];
  26.             const savedContainer = document.getElementsByClassName('saved-container')[0];
  27.  
  28.             // Create main div to wrap all the info -> img and h2s
  29.             let newDiv = document.createElement('div');
  30.             newDiv.classList.add('hits-info');
  31.             // Create img tag and add img info
  32.             let img = document.createElement('img');
  33.             img.src = './static/img/img.png';
  34.  
  35.             // Create 4 h2 and add info -> genre , name , author , date
  36.             let genreH2 = document.createElement('h2');
  37.             genreH2.textContent = "Genre:" + " " + genreInput.value;
  38.  
  39.             let nameH2 = document.createElement('h2');
  40.             nameH2.textContent = "Name:" + " " + nameInput.value;
  41.  
  42.             let authorH2 = document.createElement('h2');
  43.             authorH2.textContent = "Author:" + " " + authorInput.value;
  44.  
  45.             let dateH3 = document.createElement('h3');
  46.             dateH3.textContent = "Date:" + " " + dateInput.value;
  47.  
  48.             // Create 3 buttons with class names
  49.             let saveBtn = document.createElement('button');
  50.             saveBtn.classList.add('save-btn');
  51.             saveBtn.textContent = 'Save song';
  52.  
  53.             let likeBtn = document.createElement('button');
  54.             likeBtn.classList.add('like-btn');
  55.             likeBtn.textContent = 'Like song';
  56.  
  57.             let delBtn = document.createElement('button');
  58.             delBtn.classList.add('delete-btn');
  59.             delBtn.textContent = 'Delete';
  60.  
  61.             // ADD all info to the div and then div to the main container
  62.             newDiv.appendChild(img);
  63.             newDiv.appendChild(genreH2);
  64.             newDiv.appendChild(nameH2);
  65.             newDiv.appendChild(authorH2);
  66.             newDiv.appendChild(dateH3);
  67.             // ADD buttons
  68.             newDiv.appendChild(saveBtn);
  69.             newDiv.appendChild(likeBtn);
  70.             newDiv.appendChild(delBtn);
  71.  
  72.             allHitsContainer.appendChild(newDiv);
  73.             // Clear input fields
  74.             genreInput.value = '';
  75.             nameInput.value = '';
  76.             authorInput.value = '';
  77.             dateInput.value = '';
  78.  
  79.             likeBtn.addEventListener('click', function (event) {
  80.                 event.preventDefault();
  81.                 totalLikes++;
  82.                 likes.textContent = `Total Likes: ${totalLikes}`
  83.                 likeBtn.disabled = true;
  84.  
  85.             })
  86.  
  87.             saveBtn.addEventListener('click', function (event) {
  88.                 event.preventDefault();
  89.                 savedContainer.appendChild(newDiv);
  90.                 newDiv.removeChild(saveBtn);
  91.                 newDiv.removeChild(likeBtn);
  92.             })
  93.  
  94.             delBtn.addEventListener('click', function (event) {
  95.                 event.preventDefault();
  96.                 // event.currentTarget.parentNode.remove();
  97.                 delBtn.parentElement.remove();
  98.             })
  99.         } else {
  100.             // console.log('pass');
  101.             event.preventDefault();
  102.         }
  103.     }
  104.     );
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement