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 Controller return
- Javascript
- react Quill
- javascript 함수
- JPA Insert
- react quill custom
- Spring Entity
- 텍스트가 많은 경우
- spring builder
- springbatch
- editor Quill
- react jsx if
- springbatch chunk
- react link
- Docker Windows 설치
- react forwardRef
- javascrpit 기초
- Spring JPA
- 글자 넘침
- react Page
- Spring CORS
- react react-router-dom v6
- spring
- Spring DTO
- step 테이블
- javascript 기초
- SpringBatch 스키마
- spring security
- react
Archives
- Today
- Total
천천히 알아보는 코딩공부
javascript to do 리스트 응용 (1) 만들기/삭제/localstorage 저장 본문
HTML
<body>
<form id = "todo-form">
<input type="text" placeholder="write a To Do and press" />
</form>
<script src = "js/todo.js"></script>
</body>
리스트 만들기
- HTML 리스트 태그 : https://daeseok94.tistory.com/12
html 목록 태그 (ul, ol, li)
ul, ol, li 목록을 만들 수 있음 : ordered list의 약자로, 숫자나 알파벳 등 순서가 있는 목록을 만드는 데 사용합니다 : unordered list의 약자로, 순서가 필요 없는 목록을 만듭니다 : definition list..
daeseok94.tistory.com
todo.js
const toDoForm = document.getElementById("todo-form");
const toDoInput = toDoForm.querySelector("input");
const toDoList = document.getElementById("todo-list");
function paintToDo(newTodo){
const li = document.createElement("li");
const span = document.createElement("span");
li.appendChild(span);
span.innerText = newTodo;
toDoList.appendChild(li);
}
function handleToDoSubmit(event) {
event.preventDefault();
const newTodo = toDoInput.value;
toDoInput.value = "";
paintToDo(newTodo);
}
toDoForm.addEventListener("submit", handleToDoSubmit)
- Input 박스에 추가 원하는 리스트 입력 후 Enter 시 handleToDoSubmit function가 실행된다.
- createElement(요소):
- 자바스크립트를 이용하여 문서에 HTML 요소를 추가할 수 있습니다.
- https://www.codingfactory.net/10436
- appendChild(요소):
- 선택한 요소 안에 자식 요소를 추가합니다.
- https://www.codingfactory.net/10436
- preventDefault() :
- a 태그를 눌렀을때도 href 링크로 이동하지 않게 할 경우
- form 안에 submit 역할을 하는 버튼을 눌렀어도 새로 실행하지 않게 하고싶을 경우 (submit은 작동됨)
- https://programming119.tistory.com/100
리스트 삭제하기
todo.js
const toDoForm = document.getElementById("todo-form");
const toDoInput = toDoForm.querySelector("input");
const toDoList = document.getElementById("todo-list");
function deleteToDo(event){
const li = event.target.parentElement;
li.remove();
}
function paintToDo(newTodo){
const li = document.createElement("li");
const span = document.createElement("span");
const button = document.createElement("button");
button.innerText = "✂";
button.addEventListener("click", deleteToDo)
li.appendChild(span);
li.appendChild(button);
span.innerText = newTodo;
toDoList.appendChild(li);
}
function handleToDoSubmit(event) {
event.preventDefault();
const newTodo = toDoInput.value;
toDoInput.value = "";
paintToDo(newTodo);
}
toDoForm.addEventListener("submit", handleToDoSubmit)
- 추가된 Todo list에 버튼도 같이 추가되게 하여 버튼 클릭 이벤트를 추가해서 클릭 시 deleteToDo function 실행하게 만든 예제 입니다.
- target.parentElement : 부모를 찾습니다
- remove : 요소 제거
※ 윈도우 기준 이모지 단축키는 윈도우 + . 입니다.
리스트 localStorage 저장하기
todo.js
const toDoForm = document.getElementById("todo-form");
const toDoInput = toDoForm.querySelector("input");
const toDoList = document.getElementById("todo-list");
const toDos = [];
function saveToDos(){
localStorage.setItem("todos", JSON.stringify(toDos));
}
function deleteToDo(event){
const li = event.target.parentElement;
li.remove();
}
function paintToDo(newTodo){
const li = document.createElement("li");
const span = document.createElement("span");
const button = document.createElement("button");
button.innerText = "✂";
button.addEventListener("click", deleteToDo)
li.appendChild(span);
li.appendChild(button);
span.innerText = newTodo;
toDoList.appendChild(li);
}
function handleToDoSubmit(event) {
event.preventDefault();
const newTodo = toDoInput.value;
toDoInput.value = "";
toDos.push(newTodo);
paintToDo(newTodo);
saveToDos();
}
toDoForm.addEventListener("submit", handleToDoSubmit)
- JSON.stringify
- JSON.stringift 사용안할 시 localstorage 값이 1,2,3 입력시 1,2,3
- JSON.stringift 사용할 시 localstorage 값이 1,2,3 입력시 ["1","2","3"]
- https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
JSON.stringify() - JavaScript | MDN
JSON.stringify() 메서드는 JavaScript 값이나 객체를 JSON 문자열로 변환합니다. 선택적으로, replacer를 함수로 전달할 경우 변환 전 값을 변형할 수 있고, 배열로 전달할 경우 지정한 속성만 결과에 포함
developer.mozilla.org
'JavaScript > 예제' 카테고리의 다른 글
javascript to do 리스트 응용 (3) localstorage 데이터 삭제 (0) | 2022.04.06 |
---|---|
javascript 랜덤 (0) | 2022.03.31 |
Comments