-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo.js
56 lines (50 loc) · 1.46 KB
/
todo.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
let todos = [];
function add() {
const input = document.querySelector('input');
if (input.value) {
todos.push(input.value);
input.value = '';
render();
}
}
function remove(index) {
todos.splice(index, 1);
render();
}
function edit(index) {
const content = todos[index];
const input = document.querySelector('input');
input.value = content;
input.focus();
input.setSelectionRange(0, content.length);
todos.splice(index, 1);
render();
}
function render() {
const container = document.querySelector('.tasks');
container.innerHTML = '';
for (let i = 0; i < todos.length; i++) {
let element = helper(todos[i], i);
element.setAttribute('class', 'task');
container.appendChild(element);
}
}
function helper(text, index) {
let element = document.createElement('div');
let p = document.createElement('p');
let edit = document.createElement('button');
let btn = document.createElement('button');
p.innerText = text;
edit.innerText = 'Edit';
btn.innerText = 'Delete';
element.appendChild(p);
edit.setAttribute('id', 'edit');
btn.setAttribute('id', 'delete');
let innerdiv = document.createElement('div');
innerdiv.appendChild(edit);
innerdiv.appendChild(btn);
edit.setAttribute('onclick', 'edit(' + index + ')');
btn.setAttribute('onclick', 'remove(' + index + ')');
element.appendChild(innerdiv);
return element;
}