Advertisement
EntropyStarRover

01. SoftBlog

Feb 20th, 2021
701
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.    console.log('hellothere!');
  3.    let creatorInput = document.getElementById("creator");
  4.    let titleInput = document.getElementById("title");
  5.    let categoryInput = document.getElementById("category");
  6.    let contentTextarea = document.getElementById("content");
  7.    let postsSection = Array.from(document.getElementsByTagName("section"))[1];
  8.    let archSection = Array.from(document.getElementsByClassName("archive-section"))[0];
  9.    let ol=  document.querySelector("ol");
  10.    console.log(ol)
  11.    console.log(postsSection);
  12.    console.log(archSection);
  13.  
  14.    let createBtn = Array.from(document.getElementsByClassName('btn create'))[0];
  15.    console.log(creatorInput, titleInput, categoryInput, contentTextarea, createBtn);
  16.  
  17.    createBtn.addEventListener("click", createArticle);
  18.  
  19.    function createArticle(e) {
  20.       e.preventDefault();
  21.       console.log('create clicked');
  22.       let creator = creatorInput.value;
  23.       let title = titleInput.value;
  24.       let category = categoryInput.value;
  25.       let content = contentTextarea.value;
  26.  
  27.       let artObj={creator,title,category,content}
  28.       console.log(creator, title, category, content);
  29.  
  30.       let newArt = document.createElement("article");
  31.       let titleH = document.createElement("h1");
  32.       titleH.textContent = title;
  33.  
  34.       let categoryP = document.createElement("p");
  35.       categoryP.textContent = "Category: ";
  36.       let strCategory = document.createElement("strong");
  37.       strCategory.textContent = category;
  38.       categoryP.appendChild(strCategory);
  39.  
  40.       let creatorP = document.createElement("p");
  41.       creatorP.textContent = "Creator: ";
  42.       let strCreator = document.createElement("strong");
  43.       strCreator.textContent = creator;
  44.       creatorP.appendChild(strCreator);
  45.  
  46.       let contentP = document.createElement("p");
  47.       contentP.textContent = content;
  48.  
  49.       let buttonsDiv = document.createElement("div");
  50.       buttonsDiv.className = "buttons";
  51.       let delBtn = document.createElement("button");
  52.       delBtn.classList.add("btn");
  53.       delBtn.classList.add("delete");
  54.       delBtn.textContent = "Delete";
  55.       delBtn.addEventListener("click", () => {
  56.          delThis(newArt)
  57.       });
  58.  
  59.  
  60.       let archBtn = document.createElement("button");
  61.       archBtn.classList.add("btn");
  62.       archBtn.classList.add("archive");
  63.       archBtn.textContent = "Archive";
  64.       archBtn.addEventListener("click", () => {
  65.          archThis(newArt,artObj)
  66.       });
  67.  
  68.       buttonsDiv.appendChild(delBtn);
  69.       buttonsDiv.appendChild(archBtn);
  70.  
  71.       newArt.appendChild(titleH);
  72.       newArt.appendChild(categoryP);
  73.       newArt.appendChild(creatorP);
  74.       newArt.appendChild(contentP);
  75.       newArt.appendChild(buttonsDiv);
  76.       postsSection.appendChild(newArt);
  77.  
  78.  
  79.      
  80.    }
  81.  
  82.    function delThis(art) {
  83.       art.remove()
  84.    }
  85.  
  86.    function archThis(art,obj) {
  87.       console.log(art);
  88.       console.log(obj);
  89.       let archLi=document.createElement("li");
  90.       archLi.textContent=obj.title;
  91.       ol.appendChild(archLi);
  92.       sortArts();
  93.       art.remove()
  94.    }
  95.  
  96.    function sortArts(){
  97.       let lis=Array.from(ol.querySelectorAll("li"));
  98.       lis.sort((a,b)=>a.textContent.localeCompare(b.textContent));
  99.       lis.forEach(l=>{
  100.          ol.appendChild(l)
  101.       })
  102.    }
  103. }
Advertisement
RAW Paste Data Copied
Advertisement