-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathscript.js
38 lines (33 loc) · 1.09 KB
/
script.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
33
34
35
36
37
38
/* eslint-disable no-use-before-define */
const searchRecipes = async () => {
const query = document.getElementById('search-input').value;
if (!query) return;
try {
const response = await fetch(
`https://www.themealdb.com/api/json/v1/1/search.php?s=${query}`,
);
const data = await response.json();
displayRecipes(data.meals);
} catch (error) {
console.error('Error fetching recipes:', error);
}
};
const displayRecipes = (meals) => {
const recipesContainer = document.getElementById('recipes');
recipesContainer.innerHTML = '';
if (!meals) {
recipesContainer.innerHTML = '<p>No recipes found!</p>';
return;
}
meals.forEach((meal) => {
const recipe = document.createElement('div');
recipe.className = 'recipe';
recipe.innerHTML = `
<img src="${meal.strMealThumb}" alt="${meal.strMeal}">
<h3>${meal.strMeal}</h3>
<a href="${meal.strSource}" target="_blank">View Recipe</a>
`;
recipesContainer.appendChild(recipe);
});
};
document.getElementById('search').addEventListener('click', searchRecipes);