-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
127 lines (95 loc) · 3.56 KB
/
index.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// {/* <div class="monsters">
// <div class="monster">
// <img src="https://robohash.org/6?set=set2" alt="MD. Sakib Khan" />
// <p class="name">MD. Sakib Khan</p>
// <p class="email">[email protected]</p>
// </div>
// <div class="p-5 not-found" style="display: none">
// <span>404</span>
// <h1>🧟♂️ No Monster Found 🧟♂️</h1>
// </div>
// </div>
// </div> */}
import { monsters } from './monsters.js';
console.log(monsters);
for (let monster of monsters) {
showMonsters(monster);
}
function showMonsters(monster) {
const monsterDiv = document.createElement('div');
monsterDiv.className = 'monster';
const img = document.createElement('img');
img.src = `https://robohash.org/${monster.id}?set=set2`;
img.alt = monster.name;
const name = document.createElement('p');
name.className = 'name';
name.innerText = monster.name;
const email = document.createElement('p');
email.className = 'email';
email.innerText = monster.email;
monsterDiv.append(img, name, email);
document.querySelector('.monsters').append(monsterDiv);
console.log(monster);
}
notFound();
function notFound() {
const notFoundDiv = document.createElement('div');
notFoundDiv.className = 'p-5 not-found';
notFoundDiv.style.display = 'none';
const span = document.createElement('span');
span.innerText = '404';
const h1 = document.createElement('h1');
h1.innerText = '🧟♂️ No Monster Found 🧟♂️';
notFoundDiv.append(span, h1);
document.querySelector('.monsters').append(notFoundDiv);
console.log(notFoundDiv);
}
// document.querySelector('#search-monster').
// addEventListener('keyup', function (e) {
// const keyword = e.target.value.toLowerCase();
// const monsters = document.querySelectorAll('.monster');
// let found = false;
// for (let monster of monsters) {
// const name = monster.children[1].innerText.toLowerCase();
// const email = monster.children[2].innerText.toLowerCase();
// if (name.includes(keyword) || email.includes(keyword)) {
// monster.style.display='block';
// }
// else{
// monster.style.display='none';
// }
// }
// if (!found) {
// notFound();
// }
// })
document.querySelector('#search-monster').addEventListener('keyup', function (e) {
const keyword = e.target.value.toLowerCase();
const monsters = document.querySelectorAll('.monster');
let found = false; // Flag to track if any monster matches the keyword
for (let monster of monsters) {
const name = monster.children[1].innerText.toLowerCase();
const email = monster.children[2].innerText.toLowerCase();
if (name.includes(keyword) || email.includes(keyword)) {
monster.style.display = 'block';
found = true; // At least one monster matches the keyword
} else {
monster.style.display = 'none';
}
}
// Display "No Monster Found" message if no monster matches the keyword
if (!found) {
showNotFound();
} else {
hideNotFound();
}
});
function showNotFound() {
const notFoundDiv = document.querySelector('.not-found');
notFoundDiv.style.display = 'block';
}
function hideNotFound() {
const notFoundDiv = document.querySelector('.not-found');
notFoundDiv.style.display = 'none';
}
document.querySelector('.search-monster-form').addEventListener('submit', e => { e.preventDefault(); });