-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
64 lines (52 loc) · 2.29 KB
/
script.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
window.addEventListener('load', () => {
const form = document.querySelector(".todo-list-form")
const input = document.querySelector(".todo-list-input")
const list_el = document.querySelector("#tasks");
form.addEventListener('submit', (e) => {
e.preventDefault();
const task = input.value;
const task_el = document.createElement("div");
task_el.classList.add("task");
const task_content_el = document.createElement("div");
task_content_el.classList.add("content");
task_el.appendChild(task_content_el);
const task_input_el = document.createElement("input");
task_input_el.classList.add("text");
task_input_el.type = "text";
task_input_el.value = task;
task_input_el.setAttribute("readonly", "readonly");
task_content_el.appendChild(task_input_el);
const task_actions_el = document.createElement("div");
task_actions_el.classList.add("actions");
const task_edit_el = document.createElement("button");
task_edit_el.classList.add("edit");
task_edit_el.innerHTML = "Editer";
const task_delete_el = document.createElement("button");
task_delete_el.classList.add("delete");
task_delete_el.innerHTML = "Supprimer";
task_actions_el.appendChild(task_edit_el);
task_actions_el.appendChild(task_delete_el);
task_el.appendChild(task_actions_el);
list_el.appendChild(task_el);
input.value = "";
task_edit_el.addEventListener('click', () => {
if (task_edit_el.innerText.toLowerCase() == "editer") {
task_input_el.removeAttribute("readonly");
task_input_el.focus();
task_edit_el.innerText = "Enregistrer";
}
else {
task_input_el.setAttribute("readonly", "readonly");
task_edit_el.innerText = "Editer";
}
});
task_delete_el.addEventListener('click', () => {
list_el.removeChild(task_el);
});
tasks.push(task);
localStorage.setItem('tasks', JSON.stringify(tasks));
});
});
let tasks;
tasks = JSON.parse(localStorage.getItem('tasks')) ?? [];
/* localStorage.clear() */