Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 34 additions & 9 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,36 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link rel="icon" type="image/png" href="./assets/favicon.jpeg"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
</body>
</html>
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<link rel="icon" type="image/png" href="./assets/favicon.jpeg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="index.js"></script>
<title>Szechuan Sauce</title>
</head>
<body>
<header>
<h1>Rick & Morty</h1>
<img src="./assets/rickAndMorty.png" alt="Rick and Morty" />
</header>
<ul id="all-characters"></ul>
<main class="hide">
<div id="character-info">
<h3 id="character-name">h3</h3>
<img id="character-image" />
<p id="character-status"></p>
<p id="character-location"></p>
</div>
<div id="character-comments-section">
<form>
<label for="comment"></label><br />
<input type="text" id="comment" name="comment" /><br />
<input type="submit" value="Submit" />
</form>
<ul id="character-comments-ul"></ul>
</div>
</main>
</body>
</html>
74 changes: 74 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
document.addEventListener("DOMContentLoaded", () => {
const characters = 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 location = document.querySelector("#character-location");
const title = document.querySelector("title");
const submit = document.querySelector("input[type=submit]");
const comment = document.querySelector("input[type=text]");
const commentsList = document.querySelector('#character-comments-ul')

const getCharacters = async () => {
main.classList.remove("hide");
try {
let response = await axios.get(
"https://rickandmortyapi.com/api/character/1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20"
);
// console.log(response);
let allCharacters = response.data;
// console.log(allCharacters[0].name);

allCharacters.forEach((element) => {
const li = document.createElement("li");
const charImage = document.createElement("img");
charImage.src = element.image;
charImage.id = "photo-img";
charImage.value = element.id;
const charName = document.createElement("p");
charName.textContent = element.name;
charName.value = element.id;
li.value = element.id;
li.appendChild(charImage);
li.appendChild(charName);
characters.appendChild(li);
});
} catch (err) {
console.log(`Error message: ${err}`);
}
};

getCharacters();

characters.addEventListener("click", (e) => {
main.style.display = "flex";
fetch(`https://rickandmortyapi.com/api/character/${e.target.value}`)
.then((response) => {
if (!response.ok) {
throw Error("error message: ", Error);
}
return response.json();
})
.then((character) => {
console.log(character);
name.textContent = character.name;
title.textContent = character.name;
image.src = character.image;
status.textContent = character.status;
location.textContent = character.location.name;
})
.catch((error) => {
console.log(error);
});
});

submit.addEventListener("click", (e) => {
e.preventDefault();

let li = document.createElement('li')
li.innerHTML = `<b>${title.textContent}: </b>${comment.value}`
commentsList.appendChild(li)
comment.value = ""
});
});
Loading