-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
48 lines (41 loc) · 1.21 KB
/
Copy pathscript.js
File metadata and controls
48 lines (41 loc) · 1.21 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
// Sélection des éléments
const btn = document.getElementById("loadBtn");
const list = document.getElementById("postList");
const msg = document.getElementById("message");
const search = document.getElementById("searchInput");
let posts = [];
// Afficher les posts
function showPosts(data) {
list.innerHTML = "";
if (data.length === 0) {
list.innerHTML = "<li>Aucun résultat</li>";
return;
}
data.forEach(post => {
const li = document.createElement("li");
li.textContent = post.title;
list.appendChild(li);
});
}
// Charger les posts
btn.addEventListener("click", () => {
msg.textContent = "Chargement...";
fetch("https://jsonplaceholder.typicode.com/posts?_limit=5")
.then(response => response.json())
.then(data => {
posts = data;
showPosts(posts);
msg.textContent = "Posts chargés !";
})
.catch(() => {
msg.textContent = "Erreur de chargement";
});
});
// Recherche
search.addEventListener("input", () => {
const text = search.value.toLowerCase();
const result = posts.filter(p =>
p.title.toLowerCase().includes(text)
);
showPosts(result);
});