-
Notifications
You must be signed in to change notification settings - Fork 0
/
todolist.js
86 lines (67 loc) · 2.01 KB
/
todolist.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//DOM TodoList
var inputText = document.getElementById("txt"),
items = document.getElementsByClassName("list-group-item"),
tab = [], index;
for (var i = 0; i < items.length; i++){
tab.push(items[i].innerHTML);
}
for (var i = 0; i < items.length; i++){
items[i].onclick = function(){
index = tab.indexOf(this.innerHTML);
inputText.value = this.innerHTML;
}
}
function changeColorList(color) {
// document.body.style.background = color;
}
function refreshArray() {
tab.length = 0;
items = document.getElementsByClassName("list-group-item");
for(var i = 0; i < items.length; i++){
tab.push(items[i].innerHTML);
}
}
function addLI(){
var listNode = document.getElementById("list"), textNode = document.createTextNode(inputText.value), liNode = document.createElement("li");
liNode.className = "list-group-item"
if (inputText.value == "") {
alert("He, tak kandani nek ngisi todo ojo kosongan!!!")
} else {
liNode.appendChild(textNode);
listNode.appendChild(liNode);
refreshArray();
}
liNode.onclick = function(){
index = tab.indexOf(liNode.innerHTML);
inputText.value = liNode.innerHTML;
}
}
function editLI(){
if (inputText.value == "") {
alert("He, tak kandani nek arep edit todo ojo kosongan!!!")
} else {
items[index].innerHTML = inputText.value;
refreshArray();
}
}
function deleteLI(){
refreshArray();
if(items.length > 0){
items[index].parentNode.removeChild(items[index]);
inputText.value = "";
}
}
//CONSOLE TodoList
function runConsole() {
var myTodos = ["Makan", "Minum", "Tidur"];
var txtTodo = ('My Todo : \n'+ myTodos.join('\n'));
if (confirm(`${txtTodo}\n\nTambahkan data?`)) {
myTodos.push(prompt('Tambahkan Todo '));
} else {
if (confirm(`Hapus data?`)) {
myTodos.splice(prompt('Hapus Todo '));
} else {
}
}
alert('My Todo : \n' + myTodos.join('\n'));
}