-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
59 lines (57 loc) · 1.97 KB
/
script.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
50
51
52
53
54
55
56
57
58
59
//Initial References
let movieNameRef = document.getElementById("movie-name");
let searchBtn = document.getElementById("search-btn");
let result = document.getElementById("result");
//Function to fetch data from API
let getMovie = () => {
let movieName = movieNameRef.value;
let url = `http://www.omdbapi.com/?t=${movieName}&apikey=${key}`;
//If input field is empty
if (movieName.length <= 0) {
result.innerHTML = `<h3 class="msg">Please Enter A Movie Name</h3>`;
}
//If input field is NOT empty
else {
fetch(url)
.then((resp) => resp.json())
.then((data) => {
//If movie exists in database
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>
`;
}
//If movie does NOT exists in database
else {
result.innerHTML = `<h3 class='msg'>${data.Error}</h3>`;
}
})
//If error occurs
.catch(() => {
result.innerHTML = `<h3 class="msg">Error Occured</h3>`;
});
}
};
searchBtn.addEventListener("click", getMovie);
window.addEventListener("load", getMovie);