Files
JS/02_advanced/MATERIAL/advanced-js/lesson_05/examples/displayPosts.html
2026-06-29 11:35:03 +02:00

46 lines
1.2 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Display Posts</title>
</head>
<body>
<h1>List of Posts</h1>
<ul id="post-list"></ul>
<script>
'use strict';
async function displayPosts() {
const postList = document.querySelector('#post-list');
try {
const response = await fetch('https://dummyjson.com/posts');
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();
data.posts.forEach((post) => {
const listItem = document.createElement('li');
listItem.textContent = post.title; // Display the title of each post
postList.appendChild(listItem);
});
} catch (error) {
console.error('Error fetching posts:', error);
const errorItem = document.createElement('li');
errorItem.textContent = 'Error loading posts.';
postList.appendChild(errorItem);
}
}
displayPosts();
</script>
</body>
</html>