-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04. Example Fetch json.html
45 lines (42 loc) · 1.12 KB
/
04. Example Fetch json.html
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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Twitter | rappi </title>
<link rel="stylesheet" type="text/css" href="data/style.css" />
</head>
<body>
<div class="wrap">
<h1>Fetch JSON Example</h1>
<nav>
<button id="button1">
Fetch <code>teachers.json</code>
</button>
<button id="button2">
Show number "2"
</button>
</nav>
<div id="content">
<p class="empty">Content goes here...</p>
</div>
</div>
<script>
document.querySelector('#button1').addEventListener('click', () => {
fetch('teachers.json')
.then(res => res.json())
.then(data => {
const $content = document.querySelector('#content');
$content.innerHTML = '';
data['teachers'].forEach(t => {
const $p = document.createElement('p');
$content.appendChild($p);
$p.innerHTML = 'Hello ' + t.name;
});
});
});
document.querySelector('#button2').addEventListener('click', () => {
document.querySelector('#content').innerHTML = '2';
});
</script>
</body>
</html>