-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmlRequest.js
60 lines (56 loc) · 2.24 KB
/
xmlRequest.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
function getUsers() {
let request = new XMLHttpRequest();
request.open("GET", "https://jsonplaceholder.typicode.com/users");
request.responseType = "json";
request.send();
request.onload = function () {
document.getElementById("users").innerHTML = "";
if (request.status >= 200 && request.status < 300) {
let users = request.response;
for (user of users) {
let content = `
<div class="user-box" onclick="userSelect(${user.id},this)">
<h2 class="userName">${user.username}</h2>
<h4 class="userEmail">Email : ${user.email}</h4>
</div>`;
document.getElementById("users").innerHTML += content;
}
}
else {
alert("error");
}
}
}
function getPosts(userId) {
let request = new XMLHttpRequest();
request.open("GET", `https://jsonplaceholder.typicode.com/posts?userId=${userId}`);
request.responseType = "json";
request.send();
request.onload = function () {
document.getElementById("posts").innerHTML = "";
if (request.status >= 200 && request.status < 300) {
let posts = request.response;
for (post of posts) {
let content = `
<div class="post">
<h3 class="title">${post.title}</h3>
<p class="body">${post.body}</p>
</div>`;
document.getElementById("posts").innerHTML += content;
}
}
else {
alert("error");
}
}
}
function userSelect(id, element) {
getPosts(id);
let eleSelected = document.getElementsByClassName("active");
for (ele of eleSelected) {
ele.classList.remove("active");
}
element.classList.add("active");
}
getUsers();
getPosts(1);