-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
24 lines (22 loc) · 915 Bytes
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Import stylesheets
// import './style.css';
const form = document.getElementById('defineform') as HTMLFormElement;
// const form = document.querySelector('#defineform') as HTMLFormElement;
form.onsubmit = async (event: Event) => {
event.preventDefault();
const formData = new FormData(form);
const word: string = formData.get('defineword') as string;
try {
const response = await fetch(`https://api.dictionaryapi.dev/api/v2/entries/en/${word}`);
const data = await response.json();
const definition: string = data[0]?.meanings[0]?.definitions[0]?.definition || 'Definition not found.';
const definitionElement = document.getElementById('definition');
if (definitionElement) {
definitionElement.innerText = definition;
} else {
console.error('Element with id "definition" not found');
}
} catch (error) {
console.error('Error fetching data:', error);
}
};