Advertisement
AngelKejov

SoftBlog

Oct 20th, 2022
809
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(){
  2.  
  3.    let author = document.getElementById('creator');
  4.    let title = document.getElementById('title');
  5.    let category = document.getElementById('category');
  6.    let content = document.getElementById('content');
  7.    let listOfPosts = document.getElementsByTagName('section')[1];
  8.    let ol = document.getElementsByTagName('ol')[0];
  9.  
  10.    document.getElementsByClassName('btn create')[0].addEventListener('click', (ev) => {
  11.       ev.preventDefault();
  12.  
  13.       let authorValue = author.value;
  14.       let titleValue = title.value;
  15.       let categoryValue = category.value;
  16.       let contentValue = content.value;
  17.  
  18.       if(!authorValue || !titleValue || !categoryValue || !contentValue) {
  19.          return;
  20.       }
  21.  
  22.       createDOMElements(authorValue, titleValue, categoryValue, contentValue);
  23.       clearInputFileds();
  24.    });
  25.  
  26.    function createDOMElements(authorValue, titleValue, categoryValue, contentValue) {
  27.       let article = document.createElement('article');
  28.  
  29.       let h1 = document.createElement('h1');
  30.       h1.textContent = `${titleValue}`;
  31.  
  32.       let pCategory = document.createElement('p');
  33.       let strongCategory = document.createElement('strong');
  34.       pCategory.textContent = `Category:`;
  35.       strongCategory.textContent = `${categoryValue}`;
  36.       pCategory.appendChild(strongCategory);
  37.      
  38.       let pCreator = document.createElement('p');
  39.       let strongCreator = document.createElement('strong');
  40.       pCreator.textContent = `Creator:`;
  41.       strongCreator.textContent = `${authorValue}`;
  42.       pCreator.appendChild(strongCreator);
  43.  
  44.       let pContent = document.createElement('p');
  45.       pContent.textContent = `${contentValue}`;
  46.  
  47.       let divButtons = document.createElement('div');
  48.       divButtons.classList.add('buttons');
  49.  
  50.       let deleteBTN = document.createElement('button');
  51.       deleteBTN.classList.add('btn');
  52.       deleteBTN.classList.add('delete');
  53.       deleteBTN.textContent = 'Delete';
  54.       deleteBTN.addEventListener('click', (e) => {
  55.          let currentPost = e.target.parentNode.parentNode;
  56.  
  57.          currentPost.remove();
  58.       });
  59.  
  60.       let archiveBTN = document.createElement('button');
  61.       archiveBTN.classList.add('btn');
  62.       archiveBTN.classList.add('archive');
  63.       archiveBTN.textContent = 'Archive';
  64.       archiveBTN.addEventListener('click', (e) => {
  65.          let currentPost = e.target.parentNode.parentNode;
  66.          currentPost.remove();
  67.  
  68.          let li = document.createElement('li');
  69.          li.textContent = `${titleValue}`;
  70.          
  71.  
  72.          ol.appendChild(li);
  73.       });
  74.  
  75.       divButtons.appendChild(deleteBTN);
  76.       divButtons.appendChild(archiveBTN);
  77.  
  78.       article.appendChild(h1);
  79.       article.appendChild(pCategory);
  80.       article.appendChild(pCreator);
  81.       article.appendChild(pContent);
  82.       article.appendChild(divButtons);
  83.  
  84.       listOfPosts.appendChild(article);
  85.    }
  86.  
  87.    function clearInputFileds() {
  88.       author.value = '';
  89.       title.value = '';
  90.       category.value = '';
  91.       content.value = '';
  92.    }
  93. }
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement