-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreating_table.html
More file actions
57 lines (56 loc) · 1.54 KB
/
creating_table.html
File metadata and controls
57 lines (56 loc) · 1.54 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vanilla Javascript Tutorial</title>
</head>
<body>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
</style>
<p>Getting data from an api</p>
<table style="border: 1px solid #ccc;">
<thead>
<tr>
<td>User Name</td>
<td>Email</td>
<td>Website</td>
</tr>
</thead>
<tbody id="list">
</tbody>
</table>
<button id="button">Load Table</button>
</body>
<script>
const request = new Request('https://jsonplaceholder.typicode.com/users/');
var button = document.getElementById('button');
var el = document.getElementById('list');
button.addEventListener('click', function(){
fetch(request)
.then(response => {
if (response.status === 200) {
return response.json();
} else {
throw new Error('Something went wrong on api server!');
}
})
.then(response => {
console.log(response);
response.forEach(user=>{
var layout = '<tr>';
layout += '<td>' + user.name + '</td>';
layout += '<td>' + user.email + '</td>';
layout += '<td>' + user.website + '</td>';
layout += '</tr>';
el.innerHTML += layout;
})
}).catch(error => {
console.error(error);
});
});
</script>
</html>