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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"liveServer.settings.port": 5501
}
106 changes: 106 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// get all the characters from the first page of the API as soon ss the page loads ==> DOMContentLoaded
document.addEventListener("DOMContentLoaded", async (e) => {
try {
const characters = await axios.get(
"https://rickandmortyapi.com/api/character"
);
const ul = document.querySelector("ul");
const title = document.querySelector("title");
const main = document.querySelector("main");
const form = document.querySelector("form");
const userInput = document.querySelector("#user-text");
const commentUl = document.querySelector("#character-comments-ul");

// loop the characters to get data for each
characters.data.results.forEach((character) => {
// create the li
const li = document.createElement("li");
li.setAttribute("id", "photo-img");
// image
const img = document.createElement("img");
img.src = character.image;
// can add the value to the image as well
img.value = character.id;
li.appendChild(img);
// name
const p = document.createElement("p");
p.setAttribute("id", "character-name");
p.textContent = character.name;
li.appendChild(p);
ul.appendChild(li);
});

const image = document.createElement("img");
const nameH3 = document.createElement("h3");
const pStatus = document.createElement("p");
const pLocation = document.createElement("p");

// listen for the click on the image
ul.addEventListener("click", async (e) => {
// e.target give us the img url
const selectedPic = e.target.src;
// e.target.nextElementSibling gives us the p tag
const selectedPicName = e.target.nextElementSibling.textContent;
// update title
title.textContent = `${selectedPicName}`;
// display main
main.style.visibility = "visible";


// get character info
try {
const charactersInfo = await axios.get(
`https://rickandmortyapi.com/api/character/?name=${selectedPicName}`
);
// const image = charactersInfo.data.
const status = charactersInfo.data.results[0].status;
const location = charactersInfo.data.results[0].location.name;

// display name
nameH3.textContent = selectedPicName;
main.appendChild(nameH3);
// display image
image.src = selectedPic;
main.appendChild(image);

// display status
pStatus.innerHTML = `<b>Status:</b> ${status}`;
main.appendChild(pStatus);
// display location
pLocation.innerHTML = `<b>Location:</b> ${location}`;
main.appendChild(pLocation);
// question

// listen to the submit on the form
form.addEventListener("submit", (e) => {
e.preventDefault();
// get the input.value
const comment = userInput.value;
// create li and give input.value
li = document.createElement("li");
li.innerHTML = `<b>${selectedPicName}:</b> ${comment}`;
// append li to the ul
commentUl.appendChild(li);

// input clears
userInput.value = "";
});
} catch (err) {
console.log(err);
}
});
} catch (err) {
console.log(err);
}
});

// **** N O T E S ****
// https://rickandmortyapi.com/api
// {
// "characters": "https://rickandmortyapi.com/api/character",
// "locations": "https://rickandmortyapi.com/api/location"
// }

// https://www.educative.io/edpresso/how-to-add-an-id-to-element-in-javascript
// ASSIGN AN ID ==> TAG/VARIABLE.setAttribute(‘id’,‘ID-NAME’);
// ADD MULTIPLE ATTRIBUTES ==> TAG/VARIABLE.setAttribute(‘class’, ‘CLASS-NAME’);
94 changes: 86 additions & 8 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,89 @@
<!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>
<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" />
<link rel="icon" type="image/png" href="./assets/favicon.jpeg" />
<link rel="preconnect" href="https://fonts.gstatic.com" />
<link rel="stylesheet" href="./style.css" />
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="./app.js" defer></script>
<title>Szechuan Sauce</title>
</head>
<body>
<header>
<h1>Rick & Morty</h1>
<img src="./assets/rickAndMorty.png" alt="Rick & Morty" />
</header>

<section>
<ul id="all-characters">
<!-- <li id="photo-img">
<img src="" alt="" />
<p id="character-name"></p>
</li> -->
</ul>
</section>

<main>
<form>
<p>What would this character say about Jerry? </p>
<input type="text" name="" id="user-text" />
<input type="submit" value="Comment" />
</form>

<ul id="character-comments-ul">
<!-- APPEND THE USER'S COMMENTS HERE -->
</ul>
</main>

<!-- create using javascript -->
<!-- <main> ==> RED BOX WITH WHITE BORDER, DISPLAY FLEX, FLEX DIRECTION COLUMN
<section id="character-info">
<h3>CHARACTER'S NAME</h3>
<img src="" alt="">
<p> Status: Alive</p>
<p>Location: Earth (blah blah blah) </p>
</section> -->

<!-- **- - - - CHARACTER COMMENTS - - - -** -->
<!-- <section id="character-comments-section">
<form action=""> -->
<!-- USER ENTERS THEIR COMMENTS HERE -->
<!-- <input type="text" name="" id=""> -->
<!-- USER SUBMITS COMMENTS WITH THIS BUTTON -->
<!-- do not save the user's comment -->
<!-- <input type="submit" value="Comment"> -->
<!-- On submission the input should clear -->
<!-- </form>

<ul id="character-comments-ul"> -->
<!-- APPEND THE USER'S COMMENTS HERE -->
<!-- </ul>
</section>

</main> -->
</body>
</html>

<!--
<body>
</body>
</html>
<header>
<h1>Rick & Morty</h1>
<img src="./assets/rickAndMorty.png" alt="Rick & Morty" />
</header>

<ul id="character-comments-ul">

<main>
<section id="character-info">
<h3 id="character-name">CHARACTER'S NAME</h3>
<img id="character-img" src="" alt="">
<p> Status: Alive</p>
<p>Location: Earth (blah blah blah) </p>
</section>
</main>

</body>
-->
Loading