-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
55 lines (49 loc) · 1.74 KB
/
script.js
File metadata and controls
55 lines (49 loc) · 1.74 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
const apiKey = "c942a98b"
function executeEntree(event) {
if (event && event.key === 'Enter') {
filmSearch()
}
}
function filmSearch(){
const searchInput = document.getElementById("searchInput").value.trim()
if (searchInput === "") {
alert("Veuillez entrer un titre de film.")
return
}
const url = `http://www.omdbapi.com/?apikey=${apiKey}&s=${encodeURIComponent(searchInput)}`
fetch(url)
.then(response => response.json())
.then(data =>{
afficherResultats(data.Search)
})
.catch(error=>console.log(error))
}
function afficherResultats(films) {
const filmListElement = document.getElementById("filmList")
filmListElement.innerHTML = ""
if (films && films.length > 0) {
films.forEach(film => {
// const listItem = document.createElement("article")
// listItem.textContent = film.Title
const movieElement = document.createElement("article")
const titleElement = document.createElement("h2")
titleElement.innerText = film.Title
const imageElement = document.createElement("img")
imageElement.src = film.Poster
const btnElement = document.createElement("button")
btnElement.innerText = "Details du film"
movieElement.appendChild(titleElement)
movieElement.appendChild(imageElement)
movieElement.appendChild(btnElement)
btnElement.addEventListener("click", () => {
afficherDetails(film.imdbID)
});
filmListElement.appendChild(movieElement);
});
} else {
filmListElement.textContent = "Aucun résultat trouvé."
}
}
function afficherDetails(imdbID) {
window.location.href = `film_Infos.html?imdbID=${imdbID}`
}