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
24 changes: 24 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,32 @@
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link rel="icon" type="image/png" href="./assets/favicon.jpeg"/>
<link id='style' rel='stylesheet' href='./style.css'/>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="./index.js" defer></script>
<title>Szechuan Sauce</title>
</head>
<body>
<header>
<h1>Rick & Morty</h1>
<img src="./assets/rickAndMorty.png">
</header>
<ul id='all-characters'></ul>
<main>
<section id='character-info'>
<h3 id='character-name'></h3>
<img id='character-img'>
<p id='character-status'></p>
<p id='character-location'></p>
</section>
<section id='character-comments-section'>
<form action="">
<input id='comment' type="text">
<input type="submit">
</form>
<ul id='character-comments-ul'></ul>
</section>
</main>
</body>
</html>
49 changes: 49 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const allCharacters = document.querySelector('#all-characters');
const main = document.querySelector('main');
const name = document.querySelector('#character-name');
const image = document.querySelector('#character-img');
const status = document.querySelector('#character-status');
const clocation = 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');
img.src = character.image;
img.value = character.id;
p.textContent = character.name;
li.appendChild(img);
li.appendChild(p);
allCharacters.appendChild(li);
});
} catch (error) {
console.log(error);
}
}
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.textContent = res.data.name;
image.src = res.data.image;
status.innerHTML = `<b>Status:</b> ${res.data.status}`;
clocation.innerHTML = `<b>Location:</b> ${res.data.location.name}`;
title.textContent = res.data.name;
});

form.addEventListener('submit', (e)=> {
e.preventDefault();
const comment = document.querySelector('#comment')
const cValue = comment.value;
const li = document.createElement('li');
li.innerHTML = `<b>${title.textContent}:</b> ${cValue}`;
comments.appendChild(li);
comment.value = '';
})
Loading