-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
55 lines (44 loc) · 1.44 KB
/
script.js
File metadata and controls
55 lines (44 loc) · 1.44 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
44
45
46
47
48
49
50
51
52
53
54
55
let allPosts = [];
fetch("posts.json")
.then(res => res.json())
.then(posts => {
allPosts = posts;
renderPosts(posts);
});
function renderPosts(posts) {
const container = document.querySelector(".blog-container");
container.innerHTML = "";
posts.forEach(post => {
const card = document.createElement("div");
card.classList.add("post-card");
card.onclick = () => {
window.location.href = `post.html?id=${post.id}`;
};
card.innerHTML = `
${post.image ? `<img src="${post.image}" class="post-thumb">` : ""}
<h2>${post.title}</h2>
<p class="date">${post.date}</p>
<p>${post.excerpt}</p>
<a class="read-btn" href="post.html?id=${post.id}">Read More</a>
`;
container.appendChild(card);
});
}
// Search Function
document.getElementById("searchInput").addEventListener("input", function() {
const value = this.value.toLowerCase();
const filtered = allPosts.filter(post =>
post.title.toLowerCase().includes(value) ||
post.excerpt.toLowerCase().includes(value)
);
renderPosts(filtered);
});
const searchInput = document.getElementById("searchInput"); // your existing search bar
searchInput.addEventListener("input", (e) => {
const query = e.target.value.toLowerCase();
// filter posts where any tag matches the search query
const filteredPosts = allPosts.filter(post =>
post.tags.some(tag => tag.toLowerCase().includes(query))
);
renderPosts(filteredPosts);
});