-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautocomplete.js
More file actions
39 lines (36 loc) · 1.08 KB
/
Copy pathautocomplete.js
File metadata and controls
39 lines (36 loc) · 1.08 KB
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
const search = document.getElementById("search");
const matchList = document.getElementById("matchList");
const searchStates = async (searchText) => {
const res = await fetch("./autocomplete.json");
const states = await res.json();
let matches = states.filter((state) => {
const regex = new RegExp(`^${searchText}`, "gi");
return state.name.match(regex) || state.abbr.match(regex);
});
if (search.length === 0) {
matches = [];
matchList.innerHTML = "";
}
outputHtml(matches);
};
const outputHtml = (matches) => {
if (matches.length > 0) {
const html = matches
.map(
(match) =>
`
<div class="card card-body mb-1">
<h4>
${match.name} (${match.abbr}) <span class="text-primary">${match.capital}</span>
</h4>
<small>
Lat: ${match.lat} / Long: ${match.long}
</small>
</div>
`
)
.join("");
matchList.innerHTML = html;
}
};
search.addEventListener("input", () => searchStates(search.value));