-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
36 lines (30 loc) · 888 Bytes
/
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
const results = document.querySelector("#results")
// ====> GET Request
// Get query from the form
const form = document.querySelector("#search-form")
const searchMovie = (query) => {
fetch(`https://www.omdbapi.com/?s=${query}&apikey=adf1f2d7`)
.then(response => response.json())
.then((data) => {
data.Search.forEach((result) => {
const movieTag = `<li class="list-inline-item">
<img src="${result.Poster}" alt="">
<p>${result.Title}</p>
</li>`
results.insertAdjacentHTML("beforeend", movieTag)
})
})
}
// Add an EventListener
form.addEventListener("submit", (event) => {
results.innerHTML = "";
event.preventDefault();
const input = event.currentTarget.querySelector("#keyword").value;
if (input.trim() === '') {
searchMovie("The Lion King");
}
else {
searchMovie(input);
}
});
searchMovie("The Lion King");