-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
69 lines (56 loc) · 1.9 KB
/
Copy pathindex.js
File metadata and controls
69 lines (56 loc) · 1.9 KB
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
65
66
67
68
69
const form = document.querySelector('.input-container')
const taskContent = document.querySelector('.task-content');
const listContent = document.querySelector('.list-content');
const input = document.querySelector('input');
const buttonSubmit = form.querySelector('#btnSubmit');
let elementEdit = null;
form.addEventListener('submit', (e) => {
e.preventDefault();
const formData = new FormData(e.target)
const data = Object.fromEntries(formData);
if (!data.task) return null;
const { task } = data;
// Validate to edit
if (elementEdit) {
editTask(task)
} else {
createTask(task);
}
e.target.reset();
})
const createTask = (task) => {
const liTag = document.createElement("li");
const spanTag = document.createElement('span');
const buttonTag = document.createElement('button');
const buttonTagEdit = document.createElement('button');
const actionsTag = document.createElement('div');
const headerTaskTag = document.createElement('span');
liTag.className = 'list-content';
spanTag.textContent = task;
buttonTag.className = 'btnDelete';
buttonTag.textContent = 'Delete'
buttonTagEdit.textContent = 'Edit';
buttonTagEdit.className = 'btnEdit';
actionsTag.className = 'actions';
headerTaskTag.textContent = 'Actions';
buttonTag.addEventListener('click', () => {
liTag.remove()
})
buttonTagEdit.addEventListener('click', () => {
buttonSubmit.textContent = 'Save ✔️'
input.value = spanTag.textContent;
elementEdit = liTag;
})
// For add all elements taskContent, used this content for solution
liTag.appendChild(spanTag)
actionsTag.appendChild(buttonTag)
actionsTag.appendChild(buttonTagEdit)
liTag.appendChild(actionsTag)
taskContent.appendChild(liTag)
}
const editTask = (task) => {
buttonSubmit.innerHTML = 'Add task ✔️';
elementEdit.querySelector('span').textContent = task;
elementEdit = null;
// console.log(elementEdit)
}