georgiev955

01. Forum posts

Oct 4th, 2023
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. function solve() {
  2. const titleEl = document.querySelector('#post-title');
  3. const categoryEl = document.querySelector('#post-category');
  4. const contentEl = document.querySelector('#post-content');
  5. const reviewList = document.querySelector('#review-list');
  6. const publishedList = document.querySelector('#published-list');
  7. const publishBtn = document.querySelector('#publish-btn');
  8. const clearBtn = document.querySelector('#clear-btn');
  9.  
  10. publishBtn.addEventListener('click', publishPost);
  11.  
  12. function publishPost(e) {
  13. e.preventDefault();
  14.  
  15. const title = titleEl.value;
  16. const category = categoryEl.value;
  17. const content = contentEl.value;
  18.  
  19. if (!title || !category || !content) {
  20. return;
  21. }
  22.  
  23. clearInputFields();
  24. createPostListElement(title, category, content);
  25. const editBtns = Array.from(document.querySelectorAll('.edit'));
  26. editBtns.forEach(button => button.addEventListener('click', editInfo));
  27. const approveBtns = Array.from(document.querySelectorAll('.approve'));
  28. approveBtns.forEach(button => button.addEventListener('click', approvePost));
  29. clearBtn.addEventListener('click', clearPosts);
  30. }
  31.  
  32. function clearInputFields() {
  33. titleEl.value = '';
  34. categoryEl.value = '';
  35. contentEl.value = '';
  36. }
  37.  
  38. function createPostListElement(title, category, content) {
  39. const listElement = document.createElement('li');
  40. listElement.classList.add('rpost');
  41. listElement.innerHTML = `
  42. <article>
  43. <h4>${title}</h4>
  44. <p>Category: ${category}</p>
  45. <p>Content: ${content}</p>
  46. </article>
  47. <button class="action-btn edit">Edit</button>
  48. <button class="action-btn approve">Approve</button>
  49. `
  50. reviewList.appendChild(listElement);
  51. }
  52.  
  53. function editInfo(e) {
  54. const listElRef = e.target.parentElement;
  55. const data = listElRef.children[0].children;
  56. titleEl.value = data[0].textContent
  57. categoryEl.value = data[1].textContent.split(': ')[1];
  58. contentEl.value = data[2].textContent.split(': ')[1];
  59. listElRef.remove();
  60. }
  61.  
  62. function approvePost(e) {
  63. const listElRef = e.target.parentElement;
  64. listElRef.children[1].remove()
  65. listElRef.children[1].remove()
  66. publishedList.appendChild(listElRef);
  67. }
  68.  
  69. function clearPosts(e) {
  70. const elementsToDelete = Array.from(publishedList.children);
  71. for (const el of elementsToDelete) {
  72. el.remove();
  73. }
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment