Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function mySolution() {
  2.   let button = document.querySelector("#inputSection button");
  3.   button.addEventListener("click", addQuestion);
  4.  
  5.   function addQuestion() {
  6.     let textArea = document.querySelector("#inputSection textarea").value;
  7.     //check if the thextarea is filled
  8.     if (textArea) {
  9.       //get the username value
  10.       let username = document.querySelector("#inputSection input").value;
  11.       if (!username) {
  12.         username = "Anonymous";
  13.       }
  14.       createPendingQuestion(textArea, username);
  15.     }
  16.     function createPendingQuestion(textArea, username) {
  17.       //create the wrapper
  18.       let wrapper = document.createElement("div");
  19.       wrapper.setAttribute("class", "pendingQuestion");
  20.  
  21.       //create the first child element - image
  22.       let imageElement = document.createElement("img");
  23.       imageElement.setAttribute("src", "./images/user.png");
  24.       imageElement.setAttribute("width", "32");
  25.       imageElement.setAttribute("height", "32");
  26.  
  27.       //create the second child element - span
  28.       let spanElement = document.createElement("span");
  29.       spanElement.textContent = username;
  30.  
  31.       //create the third child element - paragraph
  32.       let pElement = document.createElement("p");
  33.       pElement.textContent = textArea;
  34.  
  35.       //create the fourth child element - div which contains archive and open buttons
  36.       let actionsDivElement = document.createElement("div");
  37.       actionsDivElement.setAttribute("class", "actions");
  38.  
  39.       //create the archive button
  40.       let archiveButtonElement = document.createElement("button");
  41.       archiveButtonElement.setAttribute("class", "archive");
  42.       archiveButtonElement.textContent = "Archive";
  43.       archiveButtonElement.addEventListener("click", function() {
  44.         document.getElementById("pendingQuestions").removeChild(wrapper);
  45.       });
  46.       //create the open button
  47.       let openButtonElement = document.createElement("button");
  48.       openButtonElement.setAttribute("class", "open");
  49.       openButtonElement.textContent = "Open";
  50.       openButtonElement.addEventListener("click", function() {
  51.         createOpenQuestion(textArea, username);
  52.         document.getElementById("pendingQuestions").removeChild(wrapper);
  53.       });
  54.  
  55.       //append the action buttons to the action div
  56.       actionsDivElement.appendChild(archiveButtonElement);
  57.       actionsDivElement.appendChild(openButtonElement);
  58.  
  59.       //append all children to the wrapper element
  60.       wrapper.appendChild(imageElement);
  61.       wrapper.appendChild(spanElement);
  62.       wrapper.appendChild(pElement);
  63.       wrapper.appendChild(actionsDivElement);
  64.  
  65.       //append the wrapper to the pending questions section
  66.       document.getElementById("pendingQuestions").appendChild(wrapper);
  67.     }
  68.     function createOpenQuestion(textArea, username) {
  69.       //create the open question wrapper element
  70.       let wrapper = document.createElement("div");
  71.       wrapper.setAttribute("class", "openQuestion");
  72.  
  73.       //create the first child element - image
  74.       let imageElement = document.createElement("img");
  75.       imageElement.setAttribute("src", "./images/user.png");
  76.       imageElement.setAttribute("width", "32");
  77.       imageElement.setAttribute("height", "32");
  78.  
  79.       //create the second child element - span
  80.       let spanElement = document.createElement("span");
  81.       spanElement.textContent = username;
  82.  
  83.       //create the third child element - paragraph
  84.       let pElement = document.createElement("p");
  85.       pElement.textContent = textArea;
  86.  
  87.       //create the fourth child element - div which contains archive and open buttons
  88.       let actionsDivElement = document.createElement("div");
  89.       actionsDivElement.setAttribute("class", "actions");
  90.  
  91.       //create the reply button element
  92.       let replyButtonElement = document.createElement("button");
  93.       replyButtonElement.setAttribute("class", "reply");
  94.       replyButtonElement.textContent = "Reply";
  95.       replyButtonElement.addEventListener("click", function() {
  96.         if (replySectionElement.getAttribute("style") === "display: none;") {
  97.           replySectionElement.setAttribute("style", "display: block;");
  98.           replyButtonElement.textContent = "Back";
  99.         } else {
  100.           replySectionElement.setAttribute("style", "display: none;");
  101.           replyButtonElement.textContent = "Reply";
  102.         }
  103.       });
  104.       //append the reply button to the actions div
  105.       actionsDivElement.appendChild(replyButtonElement);
  106.  
  107.       //create the reply section element
  108.       let replySectionElement = document.createElement("div");
  109.       replySectionElement.setAttribute("class", "replySection");
  110.       replySectionElement.setAttribute("style", "display: none;");
  111.       //create the reply input field element
  112.       let replyInputElement = document.createElement("input");
  113.       replyInputElement.setAttribute("class", "replyInput");
  114.       replyInputElement.setAttribute("type", "text");
  115.       replyInputElement.setAttribute(
  116.         "placeholder",
  117.         "Reply to this question here..."
  118.       );
  119.       //create the reply button
  120.       let sendButtonElement = document.createElement("button");
  121.       sendButtonElement.setAttribute("class", "replyButton");
  122.       sendButtonElement.textContent = "Send";
  123.       sendButtonElement.addEventListener("click", function() {
  124.         if (replyInputElement.value) {
  125.           //create the li element for currentAnswer
  126.           let liElement=document.createElement('li');
  127.           liElement.textContent=replyInputElement.value;
  128.           olElement.appendChild(liElement);
  129.           replyInputElement.value='';
  130.         }
  131.       });
  132.       //create the ordered list element for the reply section
  133.       let olElement = document.createElement("ol");
  134.       olElement.setAttribute("class", "reply");
  135.       olElement.setAttribute("type", 1);
  136.  
  137.       //append all section elements to the reply section
  138.       replySectionElement.appendChild(replyInputElement);
  139.       replySectionElement.appendChild(sendButtonElement);
  140.       replySectionElement.appendChild(olElement);
  141.  
  142.       //append all elements to the wrapper
  143.       wrapper.appendChild(imageElement);
  144.       wrapper.appendChild(spanElement);
  145.       wrapper.appendChild(pElement);
  146.       wrapper.appendChild(actionsDivElement);
  147.       wrapper.appendChild(replySectionElement);
  148.  
  149.       document.getElementById("openQuestions").appendChild(wrapper);
  150.     }
  151.   }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement