Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This function will be invoked when the html is loaded. Check the console in the browser or index.html file.
  2. const inputSection = document.getElementById('inputSection');
  3. const textInput = inputSection.getElementsByTagName('textarea')[0];
  4. const usernameInput = inputSection.getElementsByTagName('input')[0];
  5. const sendBtn = inputSection.getElementsByTagName('button')[0];
  6. const pendingQs = document.getElementById("pendingQuestions");
  7. const openQs = document.getElementById("openQuestions");
  8.  
  9. function mySolution() {
  10.     sendBtn.addEventListener("click", () => {
  11.         const nickName = usernameInput.value ? usernameInput.value : "Anonymous";
  12.         const pendingQuestion = createPendingQuestion(textInput.value, nickName);
  13.         pendingQs.appendChild(pendingQuestion);
  14.     })
  15. }
  16.  
  17. function createPendingQuestion(questionText, usernameText) {
  18.     var newElement = createElement("div", null, "pendingQuestion");
  19.  
  20.     var img = createElement("img");
  21.     img.src = "./images/user.png";
  22.  
  23.     var username = createElement("span", usernameText);
  24.     var question = createElement("p", questionText);
  25.  
  26.     var actions = createElement("div", null, "actions");
  27.  
  28.     var open = createElement("button", "Archive", "archive");
  29.     open.addEventListener("click", (e) => {
  30.         handleClickArchive(e);
  31.     })
  32.  
  33.     var archive = createElement("button", "Open", "open");
  34.     archive.addEventListener("click", (e) => {
  35.         handleClickOpen(e);
  36.     });
  37.  
  38.     actions.appendChild(archive);
  39.     actions.appendChild(open);
  40.     newElement.appendChild(img);
  41.     newElement.appendChild(username);
  42.     newElement.appendChild(question);
  43.     newElement.appendChild(actions);
  44.  
  45.     return newElement;
  46. }
  47.  
  48. function createElement(elementType, innerText, className) {
  49.     var newElement = document.createElement(elementType);
  50.     if (innerText) {
  51.         newElement.innerText = innerText;
  52.     }
  53.     if (className) {
  54.         newElement.classList.add(className);
  55.     }
  56.  
  57.     return newElement;
  58. }
  59.  
  60. function handleClickOpen(event) {
  61.     if (event.target.classList.contains("open")) {
  62.         event.preventDefault();
  63.         const removedPendingQuestion = getItem(event.target);
  64.         removeItem(event.target);
  65.         openQs.appendChild(removedPendingQuestion);
  66.     }
  67. }
  68.  
  69. function handleClickArchive(event) {
  70.     if (event.target.classList.contains("archive")) {
  71.         event.preventDefault();
  72.         removeItem(event.target);
  73.     }
  74. }
  75.  
  76. function removeItem(button) {
  77.     var item = getItem(button);
  78.  
  79.     if (item) {
  80.         item.parentNode.removeChild(item);
  81.     }
  82. }
  83.  
  84. function getItem(button) {
  85.     var element = button.parentNode,
  86.         item = null;
  87.  
  88.     while (element) {
  89.         if (element.className === "pendingQuestion") {
  90.             item = element;
  91.             break;
  92.         }
  93.  
  94.         element = element.parentNode;
  95.     }
  96.  
  97.     return item;
  98. }
  99. // To check out your solution, just submit mySolution() function in judge system.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement