forked from hexblend/insta-posts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
51 lines (45 loc) · 1.36 KB
/
script.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
const input = document.querySelector('.search');
const ul = document.querySelector('.results');
// prettier-ignore
const cities = ['Tokyo', 'Delhi', 'Shanghai', 'Mumbai', 'Sao Paolo', 'Beijing', 'Mexico City','New York City', 'Rio de Janeiro'];
const foundCities = [];
const genHTML = (city) => {
const result = document.createElement('li');
result.classList.add('result');
result.innerText = city;
return result;
};
let timeout = null;
input.addEventListener('keyup', (e) => {
clearTimeout(timeout);
const query = e.target.value;
// Wait for the user to stop typing
timeout = setTimeout(() => {
if (query !== '') {
// Search through the list
cities
.filter((city) => city.toLowerCase().includes(query.toLowerCase()))
.forEach((city) => {
// Add city if not found already
if (foundCities.indexOf(city) === -1) {
foundCities.push(city);
ul.appendChild(genHTML(city));
}
});
// Set search on click [Optional]
document.querySelectorAll('.result').forEach((result) => {
result.addEventListener('click', () => {
input.value = result.innerText;
// Clear results
foundCities.splice(0, foundCities.length);
ul.innerHTML = '';
});
});
}
}, 500);
// Clear results
if (query === '') {
foundCities.splice(0, foundCities.length); // Clear foundCities array
ul.innerHTML = ''; // Clear the ul
}
});