-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
28 lines (24 loc) · 850 Bytes
/
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
// TODO: Autocomplete the input with AJAX calls.
const results = document.querySelector("ul#results");
const fetchAPI = (keyword) => {
const url = `https://wagon-dictionary.herokuapp.com/autocomplete/${keyword}`;
fetch(url)
.then(response => response.json())
.then((data) => {
const words = data.words.slice(0, 5);
// before inserting, empty the UL
results.innerHTML = "";
// insert each result to the DOM
words.forEach((word) => {
results.insertAdjacentHTML("beforeend", `<li>${word}</li>`);
});
});
};
// find the search bar for the inputs
const input = document.getElementById("search");
// add the event type
input.addEventListener("keyup", (event) => {
// callback function / function that returns the results of the search
const newWord = input.value;
fetchAPI(newWord);
})