forked from foocoding/JavaScript3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.html
More file actions
72 lines (61 loc) · 2.07 KB
/
search.html
File metadata and controls
72 lines (61 loc) · 2.07 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<!DOCTYPE html>
<html lang="en">
<head>
<title>TV show search</title>
<style>
#actors {
display: flex;
}
</style>
</head>
<body>
<input id="query" type="text" />
<button id="search">Search Show</button>
<div id="poster"></div>
<div id="actors"></div>
<script>
document.getElementById('search').addEventListener('click', () => {
const inputText = document.getElementById('query').value;
apiShowSearch(inputText);
});
function apiShowSearch(searchQuery) {
const xmlReq = new XMLHttpRequest();
xmlReq.addEventListener('load', event => {
const response = JSON.parse(event.currentTarget.response);
displayShowPoster(response);
getActors(response[0].show.id);
});
xmlReq.open('GET', `http://api.tvmaze.com/search/shows?q=${searchQuery}`, true);
xmlReq.send();
}
function displayShowPoster(showResultsArr) {
const topResult = showResultsArr[0].show;
const posterDiv = document.getElementById('poster');
posterDiv.innerHTML = '';
const imageEl = document.createElement('img');
imageEl.src = topResult.image.original;
imageEl.width = '200';
posterDiv.appendChild(imageEl);
}
function getActors(showId) {
const xmlReq = new XMLHttpRequest();
xmlReq.addEventListener('load', event => {
const response = JSON.parse(event.currentTarget.response);
displayActorHeadshots(response);
});
xmlReq.open('GET', `http://api.tvmaze.com/shows/${showId}/cast`, true);
xmlReq.send();
}
function displayActorHeadshots(castData) {
const actorImagesEl = document.getElementById('actors');
actorImagesEl.innerHTML = '';
for (let castMember of castData) {
const imageEl = document.createElement('img');
imageEl.src = castMember.person.image.original;
imageEl.width = '100';
actorImagesEl.appendChild(imageEl);
}
}
</script>
</body>
</html>