-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
38 lines (32 loc) · 991 Bytes
/
main.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
const url = 'https://api.datamuse.com/words?sl=';
// Selects page elements
const inputField = document.querySelector('#input');
const submit = document.querySelector('#submit');
const responseField = document.querySelector('#responseField');
// Asynchronous function
const getSuggestions = () => {
const wordQuery = inputField.value;
const endpoint = `${url}${wordQuery}`;
fetch(endpoint).then(response => {
if (response.ok) {
return response.json();
}
throw new Error('Request failed!');
}, networkError => {
console.log(networkError.message)
})
.then(jsonResponse => {
//renderRawResponse(jsonResponse)
renderResponse(jsonResponse)
}
)
}
// Clears previous results and display results to webpage
const displaySuggestions = (event) => {
event.preventDefault();
while(responseField.firstChild){
responseField.removeChild(responseField.firstChild);
}
getSuggestions();
};
submit.addEventListener('click', displaySuggestions);