[miniProjects] 49_TodoList

๋ณด๋ฆฌยท2023๋…„ 8์›” 11์ผ
0

miniProjects

๋ชฉ๋ก ๋ณด๊ธฐ
47/47

49_TodoList

๐Ÿ’ป ์ฃผ์ œ : ํˆฌ๋‘ ๋ฆฌ์ŠคํŠธ, ๋กœ์ปฌ ์ €์žฅ์†Œ์— ์ €์žฅ

  • ์™ผ์ชฝ ํด๋ฆญ : ์™„๋ฃŒ
  • ์˜ค๋ฅธ์ชฝ ํด๋ฆญ : ์‚ญ์ œ


ํ•  ์ผ์„ ์ž…๋ ฅํ•˜๋ฉด ๋ฐฐ์—ด์— ์ €์žฅ๋œ๋‹ค.

โœจJS

form.addEventListener('submit', (e) => {
  // ์ด๋ฒคํŠธ ๊ฐœ์ฒด๋ฅผ ์˜ˆ์ง€ํ•ด ๊ธฐ๋ณธ๊ฐ’ ํ˜ธ์ถœ ๋ฐฉ์ง€
  e.preventDefault()

  addTodo()
})

function addTodo(todo) {
  // input์— ์ž…๋ ฅ๋œ value๊ฐ’์„ todoText์— ์ €์žฅ
  let todoText = input.value

  if(todo) {
    todoText = todo.text
  }

  if(todoText) {
    const todoEl = document.createElement('li')
    if(todo && todo.completed) {
      todoEl.classList.add('completed')
    }
    todoEl.innerText = todoText

    todoEl.addEventListener('click', () => todoEl.classList.toggle('completed'))
    // ์˜ค๋ฅธ์ชฝํด๋ฆญ
    todoEl.addEventListener('contextmenu', (e) => {
      e.preventDefault()

      todoEl.remove()
      updateLS()
    })

    todosUL.appendChild(todoEl)

    input.value=''

    updateLS()
  }
}

function updateLS() {
  todosEl = document.querySelectorAll('li')

  const todos = []

  todosEl.forEach(todoEl => {
      todos.push({
          text: todoEl.innerText,
          completed: todoEl.classList.contains('completed')
      })
  })

  localStorage.setItem('todos', JSON.stringify(todos))
}

// localStorage.setItem('name', JSON.stringify(obj))
// JSON.parse(localStorage.getItem(obj))
const todos = JSON.parse(localStorage.getItem('todos'))

if(todos) {
  todos.forEach(todo => addTodo(todo));
}
profile
์ •์‹ ์ฐจ๋ ค ์ด ๊ฐ๋ฐ•ํ•œ ์„ธ์ƒ์†์—์„œ

0๊ฐœ์˜ ๋Œ“๊ธ€