-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
81 lines (60 loc) · 2.27 KB
/
app.js
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
const formulario = document.getElementById("formulario");
const inputNombre = document.getElementById("nombre");
const card = document.getElementById("card");
const btnTodos = document.getElementById("todos");
const templateCard = document.getElementById("template-card").content;
const fragment = document.createDocumentFragment();
const urlCharacters = "https://bobsburgers-api.herokuapp.com/characters"
async function getCharactersByName(name) {
const urlFetch = urlCharacters + "?name=" +name;
const response = await fetch(urlFetch);
const json = await response.json();
return json;
}
async function getCharacters() {
const urlFetch2 = urlCharacters;
const response = await fetch(urlFetch2);
const json2 = await response.json();
return json2;
}
const pintarCards = characters => {
card.innerHTML= " ";
characters.forEach(element =>{
//cambiamos partes de la plantilla para cada personaje
templateCard.querySelector("img").setAttribute("src",element.image)
templateCard.querySelector("h4").textContent = element.name;
templateCard.querySelector(".card-text").textContent = element.gender;
templateCard.querySelector(".color-pelo").textContent = element.hairColor;
templateCard.querySelector(".ocupacion").textContent = element.occupation;
templateCard.querySelector("a").textContent = element.firstEpisode;
//clonamos la plantilla y la agregamos a un fragmento que ira acumulando todas las cards
const clone = templateCard.cloneNode(true);
fragment.appendChild(clone);
})
card.appendChild(fragment);
}
formulario.addEventListener("submit", e => {
e.preventDefault();
const name = inputNombre.value.trim();
if (name) {
getCharactersByName(name)
.then(characters => {
console.log(characters)
characters.forEach(element => {
console.log(element.name)
});
pintarCards(characters);
});
}
});
btnTodos.addEventListener("click", e => {
e.preventDefault();
getCharacters()
.then(characters => {
console.log(characters)
characters.forEach(element => {
console.log(element.name)
});
pintarCards(characters);
});
});