-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrupp-7.js
58 lines (50 loc) · 1.73 KB
/
grupp-7.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
const elForm = document.querySelector("#nameForm");
function getMember(event) {
document.querySelector("#output").innerHTML = "";
const value = document.querySelector("#name").value;
fetch("members.json")
.then((response) => response.json())
.then((data) => {
// Prints all information of the searched person
const result = data.filter(
(person) =>
person.firstname.toLowerCase() === value.trim().toLowerCase()
);
if (result.length > 0) {
// console.log(Object.keys(result[0]));
Object.keys(result[0]).map((k) => {
if (k === "image") {
document.querySelector("#image").src = result[0].image;
} else {
document.querySelector(
"#output"
).innerHTML += `<span>${k}: </span>${result[0][k]}<br>`;
}
});
}
/*
// print all person by key{firstname,lastname,..}
data.map((person) => {
document.querySelector(
"#output"
).innerHTML += `${value}: ${person[value]}<br>`;
});
result.map((person) => console.log(person));
*/
/**
** when using this function, you can enter a firstname and the program will print out the
** fullname and image.
const result = data.filter(
(d) => d.firstname.toLowerCase() === value.trim().toLowerCase()
);
if (result.length > 0) {
document.querySelector(
"#output"
).innerHTML = `<span>**information**</span><br>${result[0].firstname} ${result[0].lastName}<br>`;
document.querySelector("#image").src = result[0].image;
}
*/
});
event.preventDefault();
}
elForm.addEventListener("submit", getMember);