-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
58 lines (52 loc) · 1.52 KB
/
app.js
File metadata and controls
58 lines (52 loc) · 1.52 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
function printTodos(listItems) {
let ul = document.getElementById("list");
ul.innerHTML = '';
let items = JSON.parse(listItems);
let length = items.length;
for(let i = 0; i < length; i++) {
let li = document.createElement("li");
li.appendChild(document.createTextNode(items[i].value));
li.onclick = removeItemAndGetList;
ul.appendChild(li);
}
}
function getList() {
let xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
printTodos(this.responseText);
}
};
xhttp.open("GET", "http://css.serveo.net/api/getList", true);
xhttp.send();
}
function addItem() {
event.preventDefault();
let inputField = document.getElementById('itemInput');
let item = inputField.value;
inputField.value = '';
let xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
printTodos(this.responseText);
}
};
let url = URLify("http://css.serveo.net/api/addItem/" + item);
xhttp.open("POST", url, true);
xhttp.send();
}
function removeItemAndGetList(e) {
let xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
printTodos(this.responseText);
}
};
let url = URLify("http://css.serveo.net/api/removeItem/" + e.target.innerHTML);
xhttp.open("POST", url, true);
xhttp.send();
}
function URLify(string) {
return string.trim().replace(/\s/g, '%20');
}
getList();