Advertisement
Guest User

Untitled

a guest
Oct 13th, 2022
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>TODO</title>
  8. <script src="https://cdn.tailwindcss.com"></script>
  9. </head>
  10. <body class="bg-[#DCDAFF] text-white">
  11. <div class="container mx-auto my-4 max-w-[420px] px-4 md:px-0">
  12. <div class="flex flex-col items-center gap-4 relative">
  13. <div class="p-8 w-full text-center shadow rounded bg-[#A29BFE]">
  14. <h1 class="text-3xl">todo lista</h1>
  15. </div>
  16. <div class="p-8 w-full shadow rounded bg-[#ffffff] text-[#2d3436]">
  17. <ol id="todo-items" class="flex flex-col gap-4">
  18. <li id="todo-item-template" class="hidden flex justify-between group">
  19. <span></span>
  20. <button class="todo-remove scale-75 hidden group-hover:flex">
  21. <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-trash-2"><polyline points="3 6 5 6 21 6"></polyline><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"></path><line x1="10" y1="11" x2="10" y2="17"></line><line x1="14" y1="11" x2="14" y2="17"></line></svg>
  22. </button>
  23. </li>
  24. </ol>
  25. </div>
  26. <div class="absolute bottom-[-20px]">
  27. <form id="todo-form" class="flex rounded-full shadow bg-[#A29BFE]">
  28. <input type="text" class="px-6 py-2 w-[10rem] bg-transparent placeholder-white outline-none" placeholder="NapiĊĦi nekaj...">
  29. <button class="px-3">
  30. <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-plus-circle"><circle cx="12" cy="12" r="10"></circle><line x1="12" y1="8" x2="12" y2="16"></line><line x1="8" y1="12" x2="16" y2="12"></line></svg>
  31. </button>
  32. </form>
  33. </div>
  34. </div>
  35. </div>
  36.  
  37. <script>
  38. const itemsList = document.querySelector('#todo-items');
  39.  
  40. const appendItem = (todo) => {
  41. const itemTemplate = document.querySelector('#todo-item-template');
  42. const item = itemTemplate.cloneNode(true);
  43.  
  44. item.removeAttribute('id');
  45. item.classList.remove('hidden');
  46. item.setAttribute('data-id', todo.id);
  47. item.querySelector('span').innerHTML = todo.value;
  48. attachDeleteClickListener(item.querySelector('button'));
  49. itemsList.appendChild(item);
  50. }
  51.  
  52. const addToStorage = (value) => {
  53. const items = JSON.parse(localStorage.getItem('todo')) || [];
  54. items.push(value);
  55. localStorage.setItem('todo', JSON.stringify(items));
  56. }
  57.  
  58. const removeFromStorage = (id) => {
  59. const items = JSON.parse(localStorage.getItem('todo')) || [];
  60. const removed = items.filter((todo) => todo.id != id);
  61. localStorage.setItem('todo', JSON.stringify(removed));
  62. }
  63.  
  64. const loadFromStorage = () => {
  65. const items = JSON.parse(localStorage.getItem('todo')) || [];
  66.  
  67. for(const item of items) {
  68. appendItem(item);
  69. }
  70. }
  71.  
  72. const removeItem = (id) => {
  73. itemsList.querySelector(`li[data-id="${id}"]`).remove();
  74. removeFromStorage(id);
  75. }
  76.  
  77. const attachDeleteClickListener = (button) => {
  78. const id = button.parentElement.getAttribute('data-id');
  79. button.addEventListener('click', () => removeItem(id));
  80. }
  81.  
  82. const form = document.querySelector('#todo-form');
  83.  
  84. form.addEventListener('submit', (event) => {
  85. event.preventDefault();
  86.  
  87. const input = form.querySelector('input');
  88. const value = input.value;
  89.  
  90. if(value == '') {
  91. return;
  92. }
  93.  
  94. input.value = '';
  95.  
  96. const todo = {
  97. id: Date.now(),
  98. value
  99. };
  100.  
  101. appendItem(todo);
  102. addToStorage(todo);
  103. });
  104.  
  105. loadFromStorage();
  106. </script>
  107. </body>
  108. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement