forked from Ajwebdevs/Restnews
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
20 lines (19 loc) · 803 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
document.getElementById('fetch-posts').addEventListener('click', fetchPosts);
function fetchPosts() {
fetch('https://jsonplaceholder.typicode.com/posts')
.then(response => response.json())
.then(data => {
const postsContainer = document.getElementById('posts-container');
postsContainer.innerHTML = '';
data.forEach(post => {
const postElement = document.createElement('div');
postElement.classList.add('post');
postElement.innerHTML = `
<h2>${post.title}</h2>
<p>${post.body}</p>
`;
postsContainer.appendChild(postElement);
});
})
.catch(error => console.error('Error fetching posts:', error));
}