-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
39 lines (33 loc) · 1.43 KB
/
main.js
File metadata and controls
39 lines (33 loc) · 1.43 KB
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
39
function getRecipe() {
const ingredient = document.getElementById('ingredient').value;
const url = "https://www.themealdb.com/api/json/v1/1/filter.php?i=" + ingredient;
fetch(url)
.then((res) => res.json())
.then((data) => {
const recipeList = document.getElementById('recipeList');
// Check if data.meals is an array
if (Array.isArray(data.meals)) {
recipeList.innerHTML = ""; // Clear previous content
data.meals.forEach((meal) => {
const recipeItem = document.createElement('div');
recipeItem.classList.add('recipeItem');
// Display recipe name
const name = document.createElement('h3');
name.textContent = meal.strMeal;
recipeItem.appendChild(name);
// Display recipe image
const image = document.createElement('img');
image.style.display = 'block'
image.src = meal.strMealThumb;
image.alt = meal.strMeal;
recipeItem.appendChild(image);
recipeList.appendChild(recipeItem);
});
} else {
recipeList.innerHTML = "No meals found";
}
})
.catch((error) => {
console.error("Error fetching data:", error);
});
}