<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>random practice</title>
<style>
table {
width: 100%;
border-collapse: collapse;
}
th, td {
padding: 8px;
text-align: left;
border-bottom: 1px solid #ddd;
}
th {
background-color: #f4f4f4;
}
</style>
</head>
<body>
<table id="table">
<thead>
<tr>
<th>ID</th>
<th>Title</th>
<th>Body</th>
</tr>
</thead>
<tbody id="tbody">
</tbody>
</table>
<script>
let xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
let result = JSON.parse(this.responseText);
result.forEach(element => {
let tBody = document.getElementById("tbody");
let id = element.id;
let title = element.title;
let body = element.body;
let tr = document.createElement("tr");
let td1 = document.createElement("td");
let td2 = document.createElement("td");
let td3 = document.createElement("td");
td1.innerHTML = id;
td2.innerHTML = title;
td3.innerHTML = body;
tr.appendChild(td1);
tr.appendChild(td2);
tr.appendChild(td3);
tBody.appendChild(tr);
});
}
}
xhttp.open("GET", "https://jsonplaceholder.typicode.com/posts", true);
xhttp.send();
</script>
</body>
</html>
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter