krasizorbov

My Blog

Oct 10th, 2020
1,155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.  
  3.     const creatorInput = document.querySelector('#creator');
  4.     const titleInput = document.querySelector('#title');
  5.     const categoryInput = document.querySelector('#category');
  6.     const contentInput = document.querySelector('#content');
  7.  
  8.     const createBtn = document.querySelector('.btn.create');
  9.     createBtn.addEventListener('click', addArticle);
  10.  
  11.     const archiveSection = document.querySelector('.archive-section>ul');
  12.  
  13.     let arr = [];
  14.  
  15.     function addArticle(e) {
  16.  
  17.         e.preventDefault();
  18.  
  19.         const creator = creatorInput.value;
  20.         const title = titleInput.value;
  21.         const category = categoryInput.value;
  22.         const content = contentInput.value;
  23.  
  24.       if (creator && title && category && content) {
  25.         let article = document.createElement('article');
  26.  
  27.         let h1 = document.createElement('h1');
  28.  
  29.         let firstP = document.createElement('p');
  30.         let secondP = document.createElement('p');
  31.         let thirdP = document.createElement('p');
  32.  
  33.  
  34.  
  35.         let archiveBtn = document.createElement('button');
  36.         let deleteBtn = document.createElement('button');
  37.  
  38.         let div = document.createElement('div');
  39.         div.classList.add('buttons');
  40.  
  41.         archiveBtn.textContent = 'Archive';
  42.         deleteBtn.textContent = 'Delete';
  43.  
  44.         archiveBtn.classList.add('btn');
  45.         archiveBtn.classList.add('archive');
  46.  
  47.         deleteBtn.classList.add('btn');
  48.         deleteBtn.classList.add('delete');
  49.  
  50.         deleteBtn.addEventListener('click', deleteArt);
  51.         archiveBtn.addEventListener('click', archiveArt);
  52.  
  53.         h1.textContent = title;
  54.  
  55.         firstP.innerHTML = `Category: <strong>${category}</strong>`;
  56.  
  57.         secondP.innerHTML = `Creator: <strong>${creator}</strong>`;
  58.  
  59.         thirdP.textContent = content;
  60.  
  61.         article.appendChild(h1);
  62.  
  63.         article.appendChild(firstP);
  64.  
  65.         article.appendChild(secondP);
  66.  
  67.         article.appendChild(thirdP);
  68.  
  69.         div.appendChild(deleteBtn);
  70.         div.appendChild(archiveBtn);
  71.  
  72.         article.appendChild(div);
  73.  
  74.         document.querySelector("main section").appendChild(article);
  75.  
  76.         function deleteArt() {
  77.             article.remove();
  78.         }
  79.  
  80.         function archiveArt() {
  81.  
  82.           let ul = document.querySelector("ul");
  83.  
  84.             arr.push(title);
  85.  
  86.             let sorted = arr.sort((a, b) => a.localeCompare(b));
  87.  
  88.         while (ul.firstChild) {
  89.             ul.removeChild(ul.firstChild);
  90.             }
  91.  
  92.        for (const element of sorted) {
  93.         let newLi = document.createElement('li');
  94.         newLi.textContent = element;
  95.         archiveSection.appendChild(newLi);
  96.             }
  97.             article.remove();
  98.         }
  99.       }
  100.     }
  101.   }
Advertisement
Add Comment
Please, Sign In to add comment