This repository has been archived on 2023-11-10. You can view files and clone it, but cannot push or open issues or pull requests.
freecodecamp-projects/7-quality-assurance/5-american-british-translator/public/index.js

32 lines
905 B
JavaScript
Raw Normal View History

2022-11-19 15:43:04 +00:00
const translateHandler = async () => {
const textArea = document.getElementById("text-input");
const localeArea = document.getElementById("locale-select");
const errorArea = document.getElementById("error-msg");
const translatedArea = document.getElementById("translated-sentence");
const stuff = {"text": textArea.value, "locale": localeArea.value};
errorArea.innerText = "";
translatedArea.innerText = "";
const data = await fetch("/api/translate", {
method: "POST",
headers: {
"Accept": "application/json",
"Content-type": "application/json"
},
body: JSON.stringify(stuff)
});
const parsed = await data.json();
if (parsed.error) {
errorArea.innerText = JSON.stringify(parsed);
return;
}
translatedArea.innerHTML = parsed.translation;
return;
};
document.getElementById("translate-btn").addEventListener("click", translateHandler)