-
Notifications
You must be signed in to change notification settings - Fork 1
/
list.js
185 lines (153 loc) · 4.99 KB
/
list.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
var hobbie_names = ["Dancing", "Listening Music", "Cricket", "Singing", "Fooseball", "Reading Novels"];
var goal_names = ["Gymming", "Tech Group", "UPSC", "Branch Change", "Internship"];
function name_to_index_hobbie(a){
var res = hobbie_names.indexOf(a);
return res;
}
function name_to_index_goal(a){
var res = goal_names.indexOf(a);
return res;
}
function add_cross(){
var hobbie_list = document.querySelectorAll("#hobbieUL li");
var i;
for(i = 0; i < hobbie_list.length;i++){
var span = document.createElement("span");
var txt = document.createTextNode("\u00D7");
span.className = "closeHobbie";
span.appendChild(txt);
hobbie_list[i].appendChild(span);
}
var goal_list = document.querySelectorAll("#goalUL li");
var i;
for(i = 0; i < goal_list.length;i++){
var span = document.createElement("span");
var txt = document.createTextNode("\u00D7");
span.className = "closeGoal";
span.appendChild(txt);
goal_list[i].appendChild(span);
}
var delete_goal = document.getElementsByClassName("closeGoal");
var i;
for(i = 0; i < delete_goal.length; i++){
delete_goal[i].onclick = function (){
var div = this.parentElement;
let inter = div.innerText;
let final = inter.substring(0, inter.length - 2);
var goal_id = name_to_index_goal(final);
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function(){
if(this.readyState==4 && this.status == 200){
console.log("Received echo");
div.style.display = "none";
}
}
xhttp.open("GET","updateList.php?g_delete_id="+(goal_id+1),true);
xhttp.send();
}
}
var delete_hobbie = document.getElementsByClassName("closeHobbie");
var i;
for(i = 0; i < delete_hobbie.length; i++){
delete_hobbie[i].onclick = function (){
var div = this.parentElement;
let inter = div.innerText;
let final = inter.substring(0, inter.length - 2);
var hobbie_id = name_to_index_hobbie(final);
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function(){
if(this.readyState==4 && this.status == 200){
console.log("Received echo");
div.style.display = "none";
}
}
xhttp.open("GET","updateList.php?h_delete_id="+(hobbie_id+1),true);
xhttp.send();
}
}
}
function add_goal() {
var li = document.createElement("li");
var inputValue = document.getElementById("goalInput").value;
var t = document.createTextNode(inputValue);
var goal_id = goal_names.indexOf(inputValue);
li.appendChild(t);
if (inputValue === '') {
alert("You must write something!");
}
else {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function(){
if(this.readyState==4 && this.status == 200){
$txt = this.responseText;
if($txt === "canAdd"){
document.getElementById("goalUL").appendChild(li);
}
if($txt === "Already added!"){alert($txt);}
}
}
xhttp.open("GET","updateList.php?g_id="+(goal_id+1),true);
xhttp.send();
}
document.getElementById("goalInput").value = "";
var span = document.createElement("span");
var txt = document.createTextNode("\u00D7");
span.className = "closeGoal";
span.appendChild(txt);
li.appendChild(span);
span.onclick = function(){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function(){
if(this.readyState==4 && this.status == 200){
var txt = this.responseText;
var div = span.parentElement;
div.style.display = "none";
}
}
xhttp.open("GET","updateList.php?g_delete_id="+(goal_id+1),false);
xhttp.send();
}
}
// Create a new list item when clicking on the "Add" button
function add_hobbie() {
var li = document.createElement("li");
var inputValue = document.getElementById("hobbieInput").value;
var t = document.createTextNode(inputValue);
var hobbie_id = hobbie_names.indexOf(inputValue);
li.appendChild(t);
if (inputValue === '') {
alert("You must write something!");
}
else {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function(){
if(this.readyState==4 && this.status == 200){
$txt = this.responseText;
if($txt === "canAdd"){
document.getElementById("hobbieUL").appendChild(li);
}
if($txt === "Already added!"){alert($txt);}
}
}
xhttp.open("GET","updateList.php?h_id="+(hobbie_id+1),true);
xhttp.send();
}
document.getElementById("hobbieInput").value = "";
var span = document.createElement("span");
var txt = document.createTextNode("\u00D7");
span.className = "closeHobbie";
span.appendChild(txt);
li.appendChild(span);
span.onclick = function(){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function(){
if(this.readyState==4 && this.status == 200){
var txt = this.responseText;
var div = span.parentElement;
div.style.display = "none";
}
}
xhttp.open("GET","updateList.php?h_delete_id="+(hobbie_id+1),false);
xhttp.send();
}
}