forked from joinpursuit/Module-2-Practice-Final-Assessment
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathricknmorty.js
More file actions
74 lines (70 loc) · 2.41 KB
/
ricknmorty.js
File metadata and controls
74 lines (70 loc) · 2.41 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
73
74
// - API endpoint
// ~20 characters
// name & image up top
// name, image, status, location in form @ bottom
// main display formatiing is none, and then with the click of the picture, the main section displays.
// comments append the the li
const carousel = document.querySelector("#all-characters");
const characterHeader = document.querySelector("#character-header");
const characterImage = document.querySelector("#character-img");
const characterStatus = document.querySelector("#character-status");
const characterLocation = document.querySelector("#character-location");
const main = document.querySelector("main");
const pageTitle = document.querySelector("title");
const commentForm = document.querySelector("form");
const commentHistory = document.querySelector("#character-comments-ul");
const characters = async () => {
try {
const res = await axios.get(
"https://rickandmortyapi.com/api/character/?page=1"
);
res.data.results.forEach((el) => {
const li = document.createElement("li");
li.classList.add("photo-img");
const img = document.createElement("img");
const p = document.createElement("p");
img.src = el.image;
p.textContent = el.name;
carousel.appendChild(li);
li.appendChild(img);
li.appendChild(p);
});
} catch (err) {
console.log(err);
}
};
characters();
carousel.addEventListener("click", async (e) => {
try {
main.style.display = "flex";
const res = await axios.get(
"https://rickandmortyapi.com/api/character/?page=1"
);
const chosenCharacter = e.target.parentElement.innerText;
pageTitle.textContent = chosenCharacter;
res.data.results.forEach((el) => {
if (el.name === chosenCharacter) {
characterHeader.textContent = el.name;
characterImage.src = el.image;
characterStatus.innerHTML = `<b>Status:</b> ${el.status}`;
characterLocation.innerHTML = `<b>Location:</b> ${el.location.name}`;
}
});
} catch (err) {
console.log(err);
}
});
commentForm.addEventListener("submit", async (e) => {
try {
e.preventDefault();
const comment = document.querySelector("#user-comment");
const li = document.createElement("li");
debugger
li.classList.add("character-comments");
li.innerHTML = `<b>${pageTitle.textContent}:</b> ${comment.value}`;
commentHistory.appendChild(li);
comment.value = "";
} catch (err) {
console.log(err);
}
});