-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
48 lines (38 loc) · 1.87 KB
/
index.js
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
46
47
48
document.addEventListener('DOMContentLoaded', () => {
const blogPostsContainer = document.getElementById('blog-posts');
fetch('blogData.json')
.then(response => response.json())
.then(data => {
data.forEach(blog => {
const blogCard = document.createElement('div');
blogCard.classList.add('card', 'mb-4');
const blogImg = document.createElement('img');
blogImg.src = blog.image;
blogImg.alt = blog.title;
blogImg.classList.add('card-img-top');
const blogBody = document.createElement('div');
blogBody.classList.add('card-body');
const blogTitle = document.createElement('h5');
blogTitle.classList.add('card-title');
blogTitle.textContent = blog.title;
const blogDate = document.createElement('small');
blogDate.classList.add('text-muted');
blogDate.textContent = blog.date;
const blogExcerpt = document.createElement('p');
blogExcerpt.classList.add('card-text');
blogExcerpt.textContent = blog.content;
const readMoreLink = document.createElement('a');
readMoreLink.href = `singleblogpage.html?id=${blog.id}`; // Change this to include the blog post ID
readMoreLink.textContent = 'Read More';
readMoreLink.classList.add('btn', 'btn-primary');
blogBody.appendChild(blogTitle);
blogBody.appendChild(blogDate);
blogBody.appendChild(blogExcerpt);
blogBody.appendChild(readMoreLink);
blogCard.appendChild(blogImg);
blogCard.appendChild(blogBody);
blogPostsContainer.appendChild(blogCard);
});
})
.catch(error => console.error('Error fetching blog data:', error));
});