Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1주차] 변지혜 미션 제출합니다. #10

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ <h3 class="donelist-title">DONE</h3>
</section>
</main>
</body>
<script src="script.js"></script>
<script src="todoList.js"></script>
</html>
Empty file removed script.js
Empty file.
38 changes: 38 additions & 0 deletions todoList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 초기화 함수
const form = document.querySelector(".todo-form");

const init = () => {
form.addEventListener("submit", addTodoItem);
};

// 할 일 추가 함수
const addTodoItem = () => {
event.preventDefault();
// input에 입력한 value를 선택하여 todoContent에 대입
const todoContent = document.querySelector(".todo-input").value;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

이 부분에서 trim() 함수를 통해 공백 input에 대한 예외 handling이 있어도 좋을 것 같습니다

if (todoContent) {
printTodoItem(todoContent);
} else {
alert("할 일을 입력하세요!");
}
};

// 입력 받은 할 일 출력
const printTodoItem = (text) => {
const todoItem = document.createElement("li");
const todoText = document.createElement("span");

todoItem.className = "todo-list-item";
todoText.className = "todo-item-text";
todoText.innerText = text;

// li에 item 추가
todoItem.appendChild(todoText);
document.querySelector(".todo-list").appendChild(todoItem);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아래 현민님께서 잘 적어주셨는데, 코드 전체적으로
`document.querySelector(".todo-list")가 반복되는 것 같아서,
js 코드 내에서 이를 변수에 저장 후 변수로 불러와서 사용해도 좋을 것 같습니다


// input 창 초기화
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

printTodoItem 함수에서 한번 더 input 을 불러와서 value를 비워주는 것 보다는 addTodoItem 함수에서 이미 선언한 input이 있으므로 여기서 printTodoItem 을 호출한 후 value 비움 처리를 하는것이 더 효율적이지 않을까 생각합니다 ㅎㅎ

document.querySelector(".todo-input").value = "";
};

// 시작 함수
init();