-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
29 lines (23 loc) · 872 Bytes
/
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
var btnTranslate = document.querySelector("#btn-translate");
var textInput = document.querySelector("#text-input");
var outputDiv = document.querySelector("#output");
const serverURL = "https://api.funtranslations.com/translate/minion.json";
function getTranslationURL(text) {
return serverURL + "?" + "text=" + text;
}
function errorhandler(error) {
console.log("error occured:", error);
alert("Somthing wrong with the server! Try again after some time.");
}
function clickEventHandler() {
var inputText = textInput.value;
//calling server for response
fetch(getTranslationURL(inputText))
.then((Response) => Response.json())
.then((json) => {
var trasnlatedText = json.contents.translated;
outputDiv.innerText = trasnlatedText; // output
})
.catch(errorhandler);
}
btnTranslate.addEventListener("click", clickEventHandler);