-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path29_fetchProducts.html
45 lines (39 loc) · 1.2 KB
/
29_fetchProducts.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 lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<h2 style="text-align: center;">Products</h2>
<table id="plist">
<tr>
<th style="padding: 5px;">ID</th>
<th style="padding: 5px;">TITLE</th>
<th style="padding: 5px;">PRICE</th>
</tr>
</table>
<script>
async function fetchData() {
const data = await fetch("https://fakestoreapi.com/products");
const products = await data.json();
const element = document.querySelector("#plist");
products.forEach((product) => {
const tr = document.createElement("tr");
const td1 = document.createElement("td");
const td2 = document.createElement("td");
const td3 = document.createElement("td");
td1.innerHTML = product.id;
td2.innerHTML = product.title;
td3.innerHTML = product.price;
tr.appendChild(td1);
tr.appendChild(td2);
tr.appendChild(td3);
element.appendChild(tr);
});
}
fetchData();
</script>
</body>
</html>