-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
41 lines (31 loc) · 1.11 KB
/
index.js
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
32
33
34
35
36
37
38
39
40
41
const handleFormSubmit = (event) => {
event.preventDefault();
const inputElement = document.getElementById("task-input");
const inputValue = inputElement.value.trim();
if (!inputValue) {
alert("入力してください。");
return;
}
const todosElement = document.getElementById("todos");
const todoListItem = createTodoListItem(inputValue);
todosElement.appendChild(todoListItem);
inputElement.value = "";
};
const createTodoListItem = (todoContent) => {
const todoListItem = document.createElement("li");
const todoDone = document.createElement("input");
todoDone.type = "checkbox";
todoDone.onchange = () => {
todoListItem.classList.toggle("checked");
};
const todoLabel = document.createElement("label");
todoLabel.innerText = todoContent;
const removeTodoButton = document.createElement("button");
removeTodoButton.innerText = "削除";
removeTodoButton.onclick = () => {
todoListItem.remove();
};
todoListItem.append(todoDone, todoLabel, removeTodoButton);
return todoListItem;
};
document.getElementById("myForm").addEventListener("submit", handleFormSubmit);