-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathposts.html
More file actions
43 lines (39 loc) · 1.33 KB
/
posts.html
File metadata and controls
43 lines (39 loc) · 1.33 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Posts</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="container">
<a href="index.html" class="back-btn">← Go Back</a>
<h1 id="user-title">Posts</h1>
<div id="posts-container" class="cards"></div>
</div>
<script>
const p = new URLSearchParams(location.search);
const userId = p.get("userId"),
userName = decodeURIComponent(p.get("userName"));
document.getElementById("user-title").textContent = userName + "'s Posts";
fetch(`https://jsonplaceholder.typicode.com/posts?userId=${userId}`)
.then((r) => r.json())
.then((posts) => {
const cont = document.getElementById("posts-container");
posts.forEach((ps) => {
const card = document.createElement("div");
card.className = "card";
card.innerHTML = `
<h2>${ps.title}</h2>
<p>${ps.body}</p>
<button onclick="viewComments(${ps.id})">View Comments</button>
`;
cont.appendChild(card);
});
});
function viewComments(postId) {
location.href = `comments.html?postId=${postId}`;
}
</script>
</body>
</html>