-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
39 lines (38 loc) · 1.06 KB
/
app.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
39
const searchBtn = document.querySelector(".searchBtn");
const input = document.getElementById("input");
const wordTxt = document.querySelector(".word");
const meaningTxt = document.querySelector(".meaning");
let inputValue;
searchBtn.addEventListener("click", () => {
inputValue = input.value;
console.log(inputValue);
wordTxt.textContent = inputValue;
getMeaning(inputValue);
});
function getMeaning(word) {
let receivedTxt;
const xhr = new XMLHttpRequest();
xhr.open(
"GET",
"https://api.dictionaryapi.dev/api/v2/entries/en/" + word,
true
);
xhr.onload = function () {
if (xhr.status === 200) {
receivedTxt = xhr.responseText;
console.log(receivedTxt);
setMeaning(receivedTxt);
}
else if(xhr.status === 404){
const display = JSON.parse(xhr.responseText);
const show = display.message;
meaningTxt.textContent = show;
}
};
xhr.send();
}
function setMeaning(objTxt) {
arrRes = JSON.parse(objTxt);
const show = arrRes[0].meanings[0].definitions[0].definition;
meaningTxt.textContent = show;
}