Advertisement
divanov94

Untitled

Oct 18th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     let inputCreator = document.getElementById('creator');
  3.     let inputTitle = document.getElementById('title');
  4.     let inputCategory = document.getElementById('category');
  5.     let inputText = document.getElementById('content');
  6.     let createBtn = document.getElementsByClassName('btn create')[0];
  7.  
  8.     let getSection=document.querySelector('.site-content section');
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.     createBtn.addEventListener('click', addNew);
  18.  
  19.     function addNew(e) {
  20.         e.preventDefault();
  21.         let author = inputCreator.value;
  22.         let title = inputTitle.value;
  23.         let category = inputCategory.value;
  24.         let content = inputText.value;
  25.  
  26.         let article = el('article', [
  27.             el('h1', title),
  28.             el('p', `Category:`),
  29.             el('strong', category),
  30.             el('p', `Creator:`, el('strong', author)),
  31.             el('p', content),
  32.             el('div', {className: 'buttons'}, [
  33.                 el('button', {className: 'btn delete'}),
  34.                 el('button', {className: 'btn archive'}),
  35.             ])
  36.  
  37.         ]);
  38.  
  39.         getSection.appendChild(article)
  40.  
  41.  
  42.  
  43.  
  44.     }
  45.  
  46.  
  47.     function el(type, content, attributes) {
  48.         const result = document.createElement(type);
  49.  
  50.         if (attributes !== undefined) {
  51.             Object.assign(result, attributes);
  52.         }
  53.  
  54.         if (Array.isArray(content)) {
  55.             content.forEach(append);
  56.         } else {
  57.             append(content);
  58.         }
  59.  
  60.         function append(node) {
  61.             if (typeof node === 'string' || typeof node === 'number') {
  62.                 node = document.createTextNode(node);
  63.             }
  64.             result.appendChild(node);
  65.         }
  66.  
  67.         return result;
  68.     }
  69.  
  70.  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement