Skip to content

Commit 8c839ea

Browse files
committed
06: working prototype.
1 parent 43aa52c commit 8c839ea

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

06 - Type Ahead/index-ZORMIT.html

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Type Ahead 👀</title>
6+
<link rel="stylesheet" href="style.css">
7+
</head>
8+
<body>
9+
10+
<form class="search-form">
11+
<input type="text" class="search" placeholder="City or State">
12+
<ul class="suggestions">
13+
<li>Filter for a city</li>
14+
<li>City, S<span class="hl">ta</span>te <span class="population">123123</span></li>
15+
<li>or a state</li>
16+
</ul>
17+
</form>
18+
<script>
19+
const endpoint = 'https://gist.githubusercontent.com/Miserlou/c5cd8364bf9b2420bb29/raw/2bf258763cdddd704f8ffd3ea9a3e81d25e2c6f6/cities.json';
20+
var cities;
21+
fetch(endpoint)
22+
.then(response => response.json())
23+
.then(json => cities=json);
24+
25+
const suggestions = document.querySelector(".suggestions");
26+
27+
function appendCity(c) {
28+
const li = document.createElement("li");
29+
li.innerHTML = `${c.city}, ${c.state} <span class="population">${c.population}</span>`;
30+
suggestions.appendChild(li);
31+
}
32+
33+
function typeahead(e) {
34+
suggestions.innerHTML = "";
35+
const found = cities.filter(c => c.city.includes(this.value)||c.state.includes(this.value));
36+
found.forEach(c => appendCity(c));
37+
}
38+
39+
const input = document.querySelector(".search");
40+
input.addEventListener('keyup', typeahead);
41+
42+
43+
</script>
44+
</body>
45+
</html>

0 commit comments

Comments
 (0)