-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
49 lines (46 loc) · 1.59 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
49
let movieNameRef = document.getElementById("movie-name");
let searchBtn = document.getElementById("search-btn");
let result = document.getElementById("result");
let getMovie = () => {
let movieName = movieNameRef.value;
let url = `https://www.omdbapi.com/?t=${movieName}&apikey=${key}`;
if (movieName.length <= 0) {
result.innerHTML = `<h3 class="msg">Please enter a movie name</h3>`;
} else {
fetch(url)
.then((res) => res.json())
.then((data) => {
if (data.Response === "True") {
result.innerHTML = `
<div class="info">
<img src=${data.Poster} class="poster" />
<div>
<h2>${data.Title}</h2>
<div class="rating">
<img src="star-icon.svg" />
<h4>${data.imdbRating}</h4>
</div>
<div class="details">
<span>${data.Rated}</span>
<span>${data.Year}</span>
<span>${data.Runtime}</span>
</div>
<div class="genre">
<div>${data.Genre.split(",").join("</div><div>")}</div>
</div>
</div>
</div>
<h3>Plot:</h3>
<p>${data.Plot}</p>
<h3>Cast:</h3>
<p>${data.Actors}</p>
`;
} else {
result.innerHTML = `<h3 class="msg">${data.Error}</h3>`;
}
})
.catch(() => (result.innerHTML = `<h3 class=""msg>Error Occured</h3>`));
}
};
searchBtn.addEventListener("click", getMovie);
window.addEventListener("load", getMovie);