generated from CodeYourFuture/tv-show-dom-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
88 lines (77 loc) · 3.12 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//You can edit ALL of the code here
function setup() {
const allEpisodes = getAllEpisodes();
makePageForEpisodes(allEpisodes);
}
function makePageForEpisodes(episodeList) {
//test page
// const rootElem = document.getElementById("root");
// rootElem.textContent = `Got ${episodeList.length} episode(s)`;
// CARD
//for in - to loop over the episodes
episodes = episodeList;
for (let episode of episodeList) {
console.log(episode);
// create a card and clone it
let card = document.querySelector(`.card`).cloneNode(true);
//removing the template visibility (this needs to remain as a placeholder for javascript)
card.classList.remove(`template`);
//update text
card.querySelector(`.summary`).innerHTML = episode.summary;
// set the card ID to the episode ID ( first args the thing I need to change, the second is its value ~ from a key value pair)
card.setAttribute(`id`, `episode` + episode.id); //not visible
//update name
card.querySelector(`.name`).innerHTML = episode.name;
// combine season number and episode number into an episode code e.g S02E07 becomes S2E7
// update season and episode Number
card.querySelector(`.season-episode`).innerHTML =
`S` + episode.season + `E` + episode.number;
//update image
card.querySelector(`img`).src = episode.image.medium;
// append other elements in the card
document.querySelector(`.wrapper`).appendChild(card);
}
}
//Show Selector
//filter shows list and search
//search support (start-of-word) using includes - search
document.querySelector(`.s-name`).addEventListener("input", function (e) {
for (let episode of episodes) {
//if episode name - includes - this value
if (episode.name.toLowerCase().includes(this.value.toLowerCase())) {
//remove the - hide class - this knows it is a class so you do not need the `.`
//Using querySelector with IDs that are numbers
document.querySelector(`#episode` + episode.id).classList.remove(`hide`);
} else {
//adds the - hide class
document.querySelector(`#episode` + episode.id).classList.add(`hide`);
}
}
});
//season selector
document.querySelector(`.s-seasons`).addEventListener("input", function (e) {
for (let episode of episodes) {
//if seaseon has this value - using parseInt() function parses a string argument and returns an integer
if (episode.season === parseInt(this.value)) {
//remove the - hide class
document.querySelector(`#episode` + episode.id).classList.remove(`hide`);
} else {
//adds the - hide class
document.querySelector(`#episode` + episode.id).classList.add(`hide`);
}
}
});
//episode selector
document.querySelector(`.s-episodes`).addEventListener("input", function (e) {
for (let episode of episodes) {
//if episode seaseon - includes - this value
if (episode.season === parseInt(this.value)) {
//remove the - hide class
document.querySelector(`#episode` + episode.id).classList.remove(`hide`);
} else {
//adds the - hide class
document.querySelector(`#episode` + episode.id).classList.add(`hide`);
}
}
});
window.onload = setup;