-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathindex.html
More file actions
125 lines (122 loc) · 4.51 KB
/
index.html
File metadata and controls
125 lines (122 loc) · 4.51 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Szechuan Sauce</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.21.1/axios.min.js" integrity="sha512-bZS47S7sPOxkjU/4Bt0zrhEtWx0y0CRkhEp8IckzK+ltifIIE9EMIMTuT/mEzoIMewUINruDBIR/jJnbguonqQ==" crossorigin="anonymous"></script>
<style>
h1{
font-family: fantasy;
}
body {
background-color: black;
display: flex;
flex-direction: column;
align-items: center;
color: white;
}
header {
width: 70%;
height: 150px;
display: flex;
justify-content: space-between;
background-color: green;
padding: 0 20px;
margin-bottom: 20px;
}
header > img {
height: 100px;
}
main {
display: none;
border: 5px solid white;
width: 70%;
}
input[type="text"] {
width: 380px;;
}
#all-characters {
display: flex;
list-style: none;
width: 70%;
padding: 0;
margin: 0;
overflow-x: scroll;
}
</style>
</head>
<body>
<header>
<h1>Rick & Morty</h1>
<img src="./assets/rickAndMorty.png" alt="rickAndMorty">
</header>
<ul id="all-characters"></ul>
<main>
<section id="character-info">
<h3 id="character-name"></h3>
<img id="character-image">
<p id="character-status"></p>
<p id="character-location"></p>
</section>
<section id="character-comments-section">
<form>
<input id="comment" type="text">
<input type="submit">
</form>
<ul id="character-comments-ul"></ul>
</section>
</main>
<script>
const allCharacters = document.querySelector("#all-characters");
const main = document.querySelector("main");
const name = document.querySelector("#character-name");
const image = document.querySelector("#character-image");
const status = document.querySelector("#character-status");
const place = document.querySelector("#character-location");
const title = document.querySelector("title");
const form = document.querySelector("form")
const comments = document.querySelector("#character-comments-ul")
const getAllCharacters = async () => {
try {
const res = await axios.get("https://rickandmortyapi.com/api/character")
res.data.results.forEach(character => {
const li = document.createElement("li")
const img = document.createElement("img")
const p = document.createElement("p")
// console.log(character
img.src = character.image
img.value = character.id
p.innerText = character.name
li.appendChild(img)
li.appendChild(p)
allCharacters.appendChild(li)
})
} catch (err) {
console.log(err)
}
}
getAllCharacters()
allCharacters.addEventListener("click", async (e) => {
main.style.display = "flex"
const url = `https://rickandmortyapi.com/api/character/${e.target.value}`;
const res = await axios.get(url);
name.innerText = res.data.name
image.src = res.data.image
status.innerHTML = `<b>Status:<b> ${res.data.status}`
place.innerHTML = `<b>Location:</b> ${res.data.location.name}`
title.innerText = res.data.name;
})
form.addEventListener("submit", (e) => {
e.preventDefault();
const comment = document.querySelector("#comment");
console.log(comment)
const li = document.createElement("li");
li.innerHTML =`<b>${title.textContent}:</b> ${comment.value}`
comments.appendChild(li);
comment.value = ""
})
</script>
</body>
</html>