-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
32 lines (27 loc) · 975 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
30
31
32
var btnTranslate = document.querySelector("#btn-translate");
var txtInput = document.querySelector("#txt-input");
var outputDiv = document.querySelector("#output-div");
const serverUrl = "https://api.funtranslations.com/translate/yoda.json"
function generateUrl(text) {
return (serverUrl + "?text=" + text);
}
function errorHandler(err){
alert("Something is wrong with server, please try later.");
console.log("error occured " + err);
}
function clickEventHandler() {
let inputText = txtInput.value;
if(inputText===""){
alert("Enter a valid text");
return;
}
fetch(generateUrl(inputText))
.then(response => response.json())
.then(function displayOutput(jsonObj) {
let translatedText = jsonObj.contents.translated;
outputDiv.innerText = translatedText;
outputDiv.setAttribute("style", "color:black;");
})
.catch(errorHandler);
}
btnTranslate.addEventListener("click", clickEventHandler);