Intended outcome: The user inputs any characters from the keyboard. They will then be used to check if the inputted string is a word from the Dictionary API. If they match with each other, the string will be in the <p> element. If not, then the inputted string will go through anotherFunction().
Questions:
- Is it possible by
keyboard.event, but not in the<input>/<textarea>, for thefetchApi()to be able to search for a real word from a string, instead of each inputted character? (Like I do not want that only until the user types the word and pressEnter, then the API starts kicking in) - If so, what can I change in the code in order to get the above intended outcome?
Thank you again, and again. Thanks a lot!
var characters = '';
var text = document.getElementById("text");
var word = result[arg0].word;
document.addEventListener('keydown', function(input) {
if (input.key >= 'a' && input.key <= 'z') {
fetchApi(input.key);
}
});
function fetchApi(word) {
let url = `https://api.dictionaryapi.dev/api/v2/entries/en/${word}`;
fetch(url).then(res => res.json()).then(result => data(result, word));
}
function data(result, word) {
if (input.key == word) {
characters += input.key;
text.innerHTML = characters;
}
else {
text.innerHTML = anotherFunction();
}
}
<p id="text"></p>