-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.js
30 lines (26 loc) · 820 Bytes
/
search.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
const resultsBox = document.querySelector(".result-box")
const inputBox = document.getElementById("input-box")
inputBox.onkeyup = function(){
let result = []
let input = inputBox.value;
if (input.length){
result = cities.filter((code)=>{
return code.toLowerCase().startsWith(input.toLowerCase())
});
}
display(result);
}
function display(result) {
const content = result.map((list)=>{
return `<li>${list}</li>`
})
resultsBox.innerHTML = `<ul>${content.join('')}</ul>`
const search_results = document.querySelectorAll("li");
for (let i = 0; i < search_results.length; i++) {
search_results[i].setAttribute("onclick", "select(this)")
}
}
function select(list) {
inputBox.value = list.innerHTML
resultsBox.innerHTML = '';
}