Advertisement
kstoyanov

01. My Blog - 23 February 2020 - exam

Oct 19th, 2020
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.   const inputs = document.querySelectorAll('section input');
  3.   const [author, title, category] = inputs;
  4.   const content = document.querySelector('#content');
  5.  
  6.   const articlesSection = document.querySelector('main section');
  7.   const archiveSectionUl = document.querySelector('.archive-section ul');
  8.  
  9.   const createBtn = document.querySelector('button[class = "btn create"]');
  10.   createBtn.addEventListener('click', createArticle);
  11.  
  12.   const archive = [];
  13.  
  14.   function createArticle(e) {
  15.     e.preventDefault();
  16.  
  17.     const article = document.createElement('article');
  18.  
  19.     const h1 = document.createElement('h1');
  20.     h1.textContent = title.value;
  21.  
  22.     const s1 = document.createElement('strong');
  23.     s1.textContent = category.value;
  24.     const p1 = document.createElement('p');
  25.     p1.textContent = 'Category: ';
  26.     p1.appendChild(s1);
  27.  
  28.     const s2 = document.createElement('strong');
  29.     s2.textContent = author.value;
  30.     const p2 = document.createElement('p');
  31.     p2.textContent = 'Creator: ';
  32.     p2.appendChild(s2);
  33.  
  34.     const p3 = document.createElement('p');
  35.     p3.textContent = content.value;
  36.  
  37.     const div = document.createElement('div');
  38.     div.className = 'buttons';
  39.  
  40.     const deleteBtn = document.createElement('button');
  41.     deleteBtn.className = 'btn delete';
  42.     deleteBtn.textContent = 'Delete';
  43.  
  44.     const archiveBtn = document.createElement('button');
  45.     archiveBtn.className = 'btn archive';
  46.     archiveBtn.textContent = 'Archive';
  47.  
  48.     deleteBtn.addEventListener('click', deleteArticle);
  49.     archiveBtn.addEventListener('click', archiveArticle);
  50.  
  51.     div.appendChild(deleteBtn);
  52.     div.appendChild(archiveBtn);
  53.  
  54.     article.appendChild(h1);
  55.     article.appendChild(p1);
  56.     article.appendChild(p2);
  57.     article.appendChild(p3);
  58.     article.appendChild(div);
  59.  
  60.     articlesSection.appendChild(article);
  61.  
  62.     author.value = '';
  63.     title.value = '';
  64.     category.value = '';
  65.     content.value = '';
  66.  
  67.     function deleteArticle(e) {
  68.       e.target.parentElement.parentElement.remove();
  69.     }
  70.  
  71.     function archiveArticle(e) {
  72.       archiveSectionUl.textContent = '';
  73.  
  74.       const currentTitle = e.target.parentElement.parentElement.firstChild;
  75.  
  76.       const li = document.createElement('li');
  77.       li.textContent = currentTitle.textContent;
  78.  
  79.       archive.push(li);
  80.       const sorted = archive.sort((a, b) => a.textContent.localeCompare(b.textContent));
  81.  
  82.       sorted.forEach((element) => {
  83.         archiveSectionUl.appendChild(element);
  84.       });
  85.  
  86.       e.target.parentElement.parentElement.remove();
  87.     }
  88.   }
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement