viligen

softBlog

Jun 19th, 2022
929
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     let [creator, title, category, content] = document.querySelectorAll(
  3.         'form input, textarea'
  4.     );
  5.     let mainSection = document.querySelector('main section');
  6.     let olArchive = document.querySelector('section.archive-section ol');
  7.  
  8.     console.log(mainSection);
  9.     let btnCreate = document.getElementsByClassName('btn create')[0];
  10.  
  11.     btnCreate.addEventListener('click', function (e) {
  12.         e.preventDefault();
  13.         let newArticle = document.createElement('article');
  14.         newArticle.innerHTML = `
  15.       <h1>${title.value}</h1>
  16.       <p>Category:<strong>${category.value}</strong></p>
  17.       <p>Creator:<strong>${creator.value}</strong></p>
  18.       <p>${content.value}</p>
  19.  
  20.       <div class="buttons">
  21.       <button class="btn delete">Delete</button>
  22.       <button class="btn archive">Archive</button>
  23.       </div>`;
  24.         let [btnDel, btnArch] = newArticle.querySelectorAll('button');
  25.  
  26.         mainSection.appendChild(newArticle);
  27.         let currentTitle = title.value;
  28.  
  29.         [creator, title, category, content].forEach((el) => (el.value = ''));
  30.  
  31.         btnDel.addEventListener('click', (e) => {
  32.             mainSection.removeChild(newArticle);
  33.         });
  34.         btnArch.addEventListener('click', (e) => {
  35.             mainSection.removeChild(newArticle);
  36.             let newLi = document.createElement('li');
  37.             newLi.textContent = currentTitle;
  38.             olArchive.appendChild(newLi);
  39.             Array.from(olArchive.children)
  40.                 .sort((a, b) => a.innerText.localeCompare(b.innerText))
  41.                 .forEach((li) => olArchive.appendChild(li));
  42.             console.log(olArchive.children);
  43.         });
  44.     });
  45. }
  46.  
Advertisement
Add Comment
Please, Sign In to add comment