Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- JPA Update\
- Spring JPA
- javascript 기초
- react Quill
- react forwardRef
- react Page
- editor Quill
- spring
- SpringBatch 스키마
- react
- spring builder
- 코드 중복제거
- react react-router-dom v6
- springbatch
- Spring Entity
- Spring CORS
- 텍스트가 많은 경우
- springbatch chunk
- javascrpit 기초
- javascript 함수
- Spring DTO
- Docker Windows 설치
- react quill custom
- step 테이블
- react jsx if
- Spring Controller return
- JPA Insert
- react link
- spring security
- Javascript
Archives
- Today
- Total
천천히 알아보는 코딩공부
javascript to do 리스트 응용 (3) localstorage 데이터 삭제 본문
todo.js
const toDoForm = document.getElementById("todo-form");
const toDoInput = toDoForm.querySelector("input");
const toDoList = document.getElementById("todo-list");
let toDos = [];
const TODOS_KEY = "todos"
function saveToDos(){
localStorage.setItem("todos", JSON.stringify(toDos));
}
function deleteToDo(event){
const li = event.target.parentElement;
todos = todos.filter(toDo => toDo.id !== parseInt(li.id));
li.remove();
saveToDos();
}
function paintToDo(newTodo){
const li = document.createElement("li");
li.id = newTodo.id;
const span = document.createElement("span");
span.innerText= newTodo.text;
const button = document.createElement("button");
button.innerText = "✂";
button.addEventListener("click", deleteToDo)
li.appendChild(span);
li.appendChild(button);
toDoList.appendChild(li);
}
function handleToDoSubmit(event) {
event.preventDefault();
const newTodo = toDoInput.value;
toDoInput.value = "";
const newTodoObj = {
text: newTodo,
id: Date.now()
}
toDos.push(newTodoObj);
paintToDo(newTodoObj);
saveToDos();
}
toDoForm.addEventListener("submit", handleToDoSubmit)
const getToDos = localStorage.getItem(TODOS_KEY);
if(getToDos !== null){
const parsedToDos = JSON.parse(getToDos);
toDos = parsedToDos;
parsedToDos.forEach(paintToDo);
// parsedToDos.forEach((item) => console.log("this is the turn of", item));
}
- fcunction paintToDo -> 버튼 클릭시 deleteToDo 라는 Function이 실행되도록 이벤트를 만들어준다
- fcunction deleteToDo -> event.target.parentElement -> 이벤트가 발생한 요소를 반환해준다.
- filter -> 지정된 조건에 맞는건만 반환 해준다 (forEach 처럼 배열 length 3개 이면 3번 실행한다)
- 이벤트 발생한 요소를 삭제 후 localStorage에 다시 저장한다.
'JavaScript > 예제' 카테고리의 다른 글
javascript to do 리스트 응용 (1) 만들기/삭제/localstorage 저장 (0) | 2022.04.03 |
---|---|
javascript 랜덤 (0) | 2022.03.31 |
Comments